我是Java新手,我正在尝试制作一个猜谜游戏。在我的一行代码中,我尝试获取用户的输入。但是,该程序似乎没有运行该行代码。为什么会发生这种事?问题出现在我的main方法中:
Scanner input = new Scanner(System.in);
System.out.println("Let's play a guessing game!");
while (true){
// get a random number from 1 to 10
int random = (int) (Math.random() * 10) + 1;
System.out.println("I am thinking of a number between 1 and 10.");
System.out.print("What do you think it is? ");
int guess = input.nextInt();
System.out.println((guess == random) ? "You are correct!":"You're wrong! The number was " + random);
System.out.print("Play again? (Y/N)");
// this line is where the problem occurred.
String play = input.nextLine();
if (play.equalsIgnoreCase("N"))
break;
}
猜出数字后,程序会忽略询问用户是否想再次玩的那一行。为什么会发生这种情况?
蝴蝶不菲
相关分类