为什么当用户键入“ N”时我的do-while循环没有终止?

时间:2019-04-01 01:19:25

标签: java

无论我选择“ Y”还是“ N”,do while循环都不会终止。

我尝试对Y和N使用各种版本的==或!=

do {
num = (int)((Math.random() * max) + 1);
System.out.println("A new secret number has been chosen.");
do {
    System.out.println("Enter a guess: ");
    guess = scan.nextInt();
    if (guess < num) {
        System.out.print("Too low. Try again.");
    } else if (guess > num) {
        System.out.print("Too high. Try again.");
    }
    tries++;
}while(guess != num);
System.out.println("You won in " + tries + " guesses.");
System.out.println("Play Again? (Y/N)");
game = scan.next();
tries = 0; }while(game != "N");

我希望它在用户键入“ N”时终止程序。

0 个答案:

没有答案