Java nextLine()重复第行
我的猜谜游戏一切正常,但当它进入询问用户是否想再次玩的部分时,它会重复问题两次。但是我发现,如果我将输入方法从nextLine()更改为next(),它不会重复这个问题。为什么呢 以下是输入和输出:Java nextLine()重复第行,java,Java,我的猜谜游戏一切正常,但当它进入询问用户是否想再次玩的部分时,它会重复问题两次。但是我发现,如果我将输入方法从nextLine()更改为next(),它不会重复这个问题。为什么呢 以下是输入和输出: I'm guessing a number between 1-10 What is your guess? 5 You were wrong. It was 3 Do you want to play again? (Y/N) Do you want to play again? (Y/N) n
I'm guessing a number between 1-10
What is your guess? 5
You were wrong. It was 3
Do you want to play again? (Y/N) Do you want to play again? (Y/N) n
下面是代码:(它是用Java编写的)
最后一个do while循环块是询问用户是否希望再次播放的部分
import java.util.Scanner;
public class GuessingGame
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
boolean keepPlaying = true;
System.out.println("Welcome to the Guessing Game!");
while (keepPlaying) {
boolean validInput = true;
int guess, number;
String answer;
number = (int) (Math.random() * 10) + 1;
System.out.println("I'm guessing a number between 1-10");
System.out.print("What is your guess? ");
do {
validInput = true;
guess = input.nextInt();
if (guess < 1 || guess > 10) {
validInput = false;
System.out.print("That is not a valid input, " +
"guess again: ");
}
} while(!validInput);
if (guess == number)
System.out.println("You guessed correct!");
if (guess != number)
System.out.println("You were wrong. It was " + number);
do {
validInput = true;
System.out.print("Do you want to play again? (Y/N) ");
answer = input.nextLine();
if (answer.equalsIgnoreCase("y"))
keepPlaying = true;
else if (answer.equalsIgnoreCase("n"))
keepPlaying = false;
else
validInput = false;
} while (!validInput);
}
}
}
import java.util.Scanner;
公开课猜谜游戏
{
公共静态void main(字符串[]args)
{
扫描仪输入=新扫描仪(System.in);
布尔值keepPlaying=true;
System.out.println(“欢迎来到猜谜游戏!”);
同时(继续播放){
布尔值validInput=true;
int猜测,数字;
字符串回答;
数字=(int)(Math.random()*10)+1;
System.out.println(“我猜一个介于1-10之间的数字”);
System.out.print(“你猜怎么着?”);
做{
validInput=true;
guess=input.nextInt();
如果(猜测<1 | |猜测>10){
validInput=假;
System.out.print(“这不是有效的输入,”+
“再猜猜看:”;
}
}而(!validInput);
如果(猜测==数字)
System.out.println(“你猜对了!”);
如果(猜测!=数字)
System.out.println(“你错了,是“+number”);
做{
validInput=true;
System.out.print(“您想再次播放吗?(Y/N)”);
answer=input.nextLine();
如果(回答。相等信号情况(“y”))
持续播放=正确;
else if(回答。相等信号情况(“n”))
持续播放=错误;
其他的
validInput=假;
}而(!validInput);
}
}
}
我改变了编码风格,因为我发现这种方式更具可读性。我相信
input.nextLine()
的输出将在行尾包含换行符,而input.next()
不会(但扫描器将保持在同一行)。这意味着输出永远不等于“y”
或“n”
。尝试以下结果:
answer = input.nextLine().trim();
在do while
循环中,您不需要nextLine()
,只需要next()
因此,改变这一点:
answer = input.nextLine();
为此:
answer = input.next();
注意,正如其他人所建议的,您可以将其转换为while
循环。原因是当您需要至少执行一次循环时,会使用do while
循环,但您不知道需要执行循环的频率。在这种情况下,这当然是可行的,但像这样的事情就足够了:
System.out.println("Do you want to play again? (Y/N) ");
answer = input.next();
while (!answer.equalsIgnoreCase("y") && !answer.equalsIgnoreCase("n")) {
System.out.println("That is not valid input. Please enter again");
answer = input.next();
}
if (answer.equalsIgnoreCase("n"))
keepPlaying = false;
只要没有输入“y”或“n”(忽略大小写),while
循环就会保持循环。一旦完成,循环就结束。if
条件会在必要时更改keepplay值,否则不会发生任何事情,而外部while
循环将再次执行(从而重新启动程序)
Edit:这解释了原始代码无法工作的原因
我应该补充一点,您的原始语句不起作用的原因是您的第一个do while
循环。在其中,您使用:
guess = input.nextInt();
这会读取行外的数字,但不会读取行的返回值,这意味着当您使用:
answer = input.nextLine();
它会立即从nextInt()
语句中检测剩余的回车。如果您不想使用我的解决方案,即只阅读next()
,您可以这样做来吞下剩下的内容:
guess = input.nextInt();
input.nextLine();
rest of code as normal...
您的问题是,
nextInt
将在int结束时停止,但将换行保留在输入缓冲区中。要使代码正确读取答案,您必须在猜测的同一行输入,如5SpaceYReturn
如果第一个
nextLine
结果只包含空格,则忽略它,在这种情况下,只需再次调用nextLine
,而不打印消息即可。问题实际上在于完全不同的代码段。在上一个循环中时guess=input.nextInt()代码>执行时,在输入中留下一个换行符。然后,当answer=input.nextLine()时代码>在第二个循环中执行,已经有一个换行等待读取,它返回一个空字符串,这将激活最后的else
和validInput=false执行代码>以重复循环(和问题)
一种解决方案是添加input.nextLine()代码>在第二个循环之前。另一种方法是使用nextLine()
读取guess
,然后将其解析为int。但这会使事情变得复杂,因为输入不能是正确的int
。再想一想,代码已经提出了这个问题。尝试输入非数字响应。那么,定义一个函数
public static int safeParseInt(String str) {
int result;
try {
result= Integer.parseInt(str) ;
} catch(NumberFormatException ex) {
result= -1 ;
}
return result ;
}
然后将第一个循环替换为:
do {
validInput= true ;
int guess= safeParseInt( input.nextLine() ) ;
if( guess < 1 || guess > 10 ) {
validInput= false ;
System.out.print("That is not a valid input, guess again: ");
}
} while( !validInput );
do{
validInput=true;
int guess=safeParseInt(input.nextLine());
如果(猜测<1 | |猜测>10){
validInput=假;
System.out.print(“这不是有效的输入,请再次猜测:”);
}
}而(!validInput);
PS:我认为do-while
循环没有任何问题。它们是语言的一部分,语法清楚地表明,在至少执行一次主体之后,将对条件进行评估。我们不需要仅仅因为其他人不知道而删除语言中有用的部分(至少是从实践中删除)。相反,如果我们真的使用它们,它们会被更好地了解 尽量不要在
时使用do。。。人们往往不使用它们,因为它们有点不同寻常。另外,试着在你的问题中加入你的输入和输出
do {
validInput= true ;
int guess= safeParseInt( input.nextLine() ) ;
if( guess < 1 || guess > 10 ) {
validInput= false ;
System.out.print("That is not a valid input, guess again: ");
}
} while( !validInput );