Java 似乎无法重复我的低-高游戏

Java 似乎无法重复我的低-高游戏,java,Java,我正在尝试开发一个简单的高低游戏,在游戏结束后询问用户是否愿意再次玩。如果我移除外循环,而内循环的逻辑正是我想要它做的,但是我不确定如何用一个外循环来包装内循环,这个外循环将再次询问播放问题,如果答案是肯定的,则将它们放回内循环。下面是我的代码 import java.util.Scanner; import java.util.Random; public class HiLoGuess { public static void main(String[] args) {

我正在尝试开发一个简单的高低游戏,在游戏结束后询问用户是否愿意再次玩。如果我移除外循环,而内循环的逻辑正是我想要它做的,但是我不确定如何用一个外循环来包装内循环,这个外循环将再次询问播放问题,如果答案是肯定的,则将它们放回内循环。下面是我的代码

import java.util.Scanner;
import java.util.Random;

public class HiLoGuess {

    public static void main(String[] args) {

        Scanner scan = new Scanner (System.in); // Creates scanner object.
        Random numb = new Random();             // Creates an instance of the random class.
        int guess = -1;                         // Placeholder for users guess.
        int answer = numb.nextInt(100)+1;       // Generates a random number for the game.
        int count = 0;                          // Placeholder for the guess counter.
        int sentinel = 0;                       // Placeholder for players answer as to whether they want to play again or not.
        String newgame = "y";

        while (newgame.equalsIgnoreCase("y"))
        {
            while (guess != sentinel && guess != answer)                //Loop that ends when user enters a zero.
            {
                System.out.println ("Enter a number between 1-100 or 0 to quit");
                guess = scan.nextInt();
                count++;

                if (guess < answer && guess > 0 )
                {
                    System.out.println("Your guess is too low, guess again");
                }
                else if (guess > answer)
                {
                    System.out.println ("Your guess is to high, guess again");
                }

                else if (guess == answer)
                {
                    System.out.println ();
                    System.out.println ("You guessed correctly, you win!!!");
                    System.out.println ("It took you " + count + " guesses");
                }
            }
            System.out.print();
            System.out.println("Play another game: y or n?");
            newgame = scan.nextLine();
        }
    }
}

替换newgame=scan.nextLine;通过这个:newgame=scan.next

您需要在while循环中初始化变量,以便将标志重置为false,并且随机生成新的结果进行猜测

    public class Game

{

 public static void main(String[] args)

 {
Scanner scan = new Scanner(System.in); // Creates scanner object.
Random numb = new Random(); // Creates an instance of the random class.

String newgame = "y";

while (newgame.equalsIgnoreCase("y")) {
  int count = 0; // Placeholder for the guess counter.
  int guess = -1; // Placeholder for users guess.
  int answer = numb.nextInt(100) + 1; // Generates a random number for the game.
  int sentinel = 0; // Placeholder for players answer as to whether they want to play again or not.

  while (guess != sentinel && guess != answer) // Loop that ends when user enters a zero.
  {
    System.out.println("Enter a number between 1-100 or 0 to quit");
    guess = scan.nextInt();
    count++;

    if (guess < answer && guess > 0) {
      System.out.println("Your guess is too low, guess again");
    } else if (guess > answer) {
      System.out.println("Your guess is to high, guess again");
    }

    else if (guess == answer) {
      System.out.println();
      System.out.println("You guessed correctly, you win!!!");
      System.out.println("It took you " + count + " guesses");
    }
  }
  System.out.println();
  System.out.println("Play another game: y or n?");
  newgame = scan.next();
}
}
}

您需要将这些初始化放入外部循环:

int guess = -1;
int answer = numb.nextInt(100)+1;
int count = 0;        

否则,它们将保留上次游戏的值,内部循环将不再执行。

您永远不会重置猜测、哨兵或答案变量

猜猜看哨兵和猜猜!=在第一次玩游戏后,答案总是计算为false,因此内部while循环在第一次玩游戏后永远不会执行

 while (guess != sentinel && guess != answer) //this is false after the first game because you don't reset variables
            { ...}
更新OP评论: 要让代码执行您想要的操作,您需要在outter和inner while循环之间添加重置,如下所示

           while (newgame.equalsIgnoreCase("y"))
            {
            guess = -1;                  
            answer = numb.nextInt(100)+1;
            count = 0;
            while (guess != sentinel && guess != answer)                //Loop that ends when user enters a zero.
                { ...}
}

我认为这不是解决办法。是否要解释原因?nextLine只返回跳过的行,而下一个标记将为您提供下一个标记,您可以编辑您的答案来解释为什么会发生这种情况?抱歉,但是我如何重置变量?我是编程新手,不确定。没问题,记得选择并接受你认为有用的“一加一”答案。将变量放入外部循环并删除最后一个system.out.print;这场比赛非常成功。非常感谢。