nelsonkkyy wrote:
i have a problem with the number guessing game.
I do not know to make it in three tries &This is all i got so far:
Here is 1 solution..
code wrote:
boolean guessedIt = false;
Random rand = new Random ();
int secretNum = rand.nextInt (10) + 1;
int tries = 0;
for (i=1; i<=3; i++)
{
if (guessedIt==false)
{
System.out.print ("Guess a number between 1 and 10 in 3 tries: ");
System.out.flush ();
String line = br.readLine ();
guess = Integer.parseInt (line);
if (guess==secretNum)
{
guessedIt=true;
tries = i;
}
}
}
if (guessedIt==true)
{
System.out.print("You guessed the secret number in ");
System.out.print(tries);
System.out.println(" tries!");
} else {
System.out.println("You did not guess the secret number within 3 tries! Sorry!");
}