java java Exercise Exercise -5 -4
summary below us in the program, Computer Kullanici'dan between 1 and 100 are willing and determined to estimate a number and the number estimated to be 5 times the right to trial We'll be right for that help. The user may enter 0 to exit without having to guess
.
number between 1 and 100 to derive the Math.random () kullabailirsiniz. Think of a number
try to guess, for a maximum of 5 attempts.
The user can 'enter the number 0 to exit before completing the trial and error.
Before insertion of each number, the program must tell the user
in the range where the number to guess. To help you, the range must '
be restricted after each attempt (how narrow it is not' specified).
If the user enters a number outside that range, this insertion
must be counted as an attempt.
In the end, is that you have guessed, is that it has decided to end early,
the program must show video on the number of times used.
To generate a random number using Math.random () which returns
a double between 0 (inclusive) and 1 (excluded).
------------------------------------------
{public class PensaNumero
public static void main (String [] args) {
/ * Kere tahmin icin 5 max 5 max sayi ve 100 * / int
MAX_TENT = 5;
MAX_NUM int = 100;
/ * tahmini 0'san baslatiyoruz, 1 ile 100 ve arasinda Birsay turetiyoruz * / int
tent = 0;
int number = (int) (Math.random () * MAX_NUM) + 1;
int min = 1;
int max = MAX_NUM;
/ * for dongusnu sekilde kullanarak bu kadar istedigimiz Dongu saglayabiliyoruz * /
for (;;) {
Console.scriviStr (
"Guess the number I thought, from" )
Console.scriviInt (min);
Console.scriviStr ("a");
Console.scriviIntero (max);
/ * the user you want to guess, without having to guess if you are going 0 entered the program * / int n = Console.leggiIntero
();
if (n == 0) {
Console.scriviStringa ("Esco");
break;}
/ * olmadigni we check whether the entered number are determined expected range, entered the numbers from 1 to 100 among the control do we do if not re-enter a number. * /
if ((n \u0026lt;min)
continue;}
/ * The user 5 times for the estimated value of each estimate 1 estimated that expanding the right. * /
tent + +;
/ * user finders won the call number * /
if (n == numero) {
Console.scriviStringa ("Indovinato!");
break;}
/ * Forecast on the right to control and we try to help you estimate the user * /
if (tent> = MAX_TENT) {
Console.scriviStr ("Hai i Tuoi esaurito");
Console.scriviInt (MAX_TENT)
Console.scriviStringa ("attempts");
Console.scriviStr ("The number was");
Console.scriviIntero (number);
return;}
Console.scriviStringa ("Try again");
if (number> n) {
min = n + 1;} else {
max = n - 1;}
}
Console . scriviStr ("You used");
Console.scriviInt (tent);
Console.scriviStr ("tentative water");
Console.scriviIntero (MAX_TENT);
}}
------------- -----------------------------
0 comments:
Post a Comment