У меня возникла проблема со следующим кодом для игры с угадыванием, заданной для 1-32. Он начинается, но как только я беру свое первое предположение, мне дают либо слишком высокий, либо слишком низкий, плюс правильный ответ. Поэтому я всегда могу получить правильный ответ в двух догадках, что не должно произойти. Я думаю, что у меня проблема с циклом while.Guessing Game, ошибка цикла
import java.util.Scanner;
import java.util.Random;
public class GuessingGame1 {
public static void main(String[] args) {
/*
* Following should create a random number
* Generated by computer between 0-32
*/
Random rng = new Random();
int value = rng.nextInt(32);
int numberOfTries = 0;
Scanner input = new Scanner (System.in);
int guess = 0;
boolean win = false;
while (win == false) {
System.out.println("Guess of a Number between 1-32, I will tell you \n " +
"if your guess is too high, too low, or correct!");
/*
* Uses makes a guess and program tells if the guess is correct,
* too high, or too low.
*/
System.out.println("Enter your guess: ");
guess = input.nextInt();
numberOfTries++;
if (guess == value){
win = true;
} else if (guess > value){
System.out.println("Your guess is too high, try again");
} else if (guess < value){
System.out.println("Your guess is too low, try again");
}
System.out.println("Yes, the number is " + value);
System.out.println("It took you " + numberOfTries + " tries");
}
}
}
ну, вы печатаете число каждый раз в цикле, потому что 'System.out.println' не находится в состоянии, например' if (guess == value) ' – SomeJavaGuy
@ neme0025 Как вы это делали? Вы решили? –
Да, я не правильно выравнивал фигурные скобки. Не могу поверить, что я пропустил это. – neme0025