Java中的扫描仪无法正常工作 [英] Scanner in Java not working
问题描述
我正在尝试编写一个非常简单的数字猜谜游戏(下面的代码).在完成一轮比赛之后,用户应该能够决定他/她是否要玩另一轮比赛.问题是,程序总是跳过最后一个问题(切勿让用户回答"y".否则,我在这里遗漏了什么?java.util.Scanner
是否有我不知道的地方?
I'm trying to write a very simple number guessing game (code is below). After 1 round is finished, the user is supposed to be able to decide whether he/she wants to play another round or not. Problem is, the program always skips the last question (never letting the user answer 'y' or otherwise. What am I missing here? Is there something about java.util.Scanner
I don't know about?
import java.util.Random;
import java.util.Scanner;
public class GuessNum {
public GuessNum() {
int numRandom = 0;
int numGuess;
int life = 5;
String want = "";
Random rand = new Random();
Scanner scan = new Scanner(System.in);
do {
int lifeLeft = 5;
numRandom = rand.nextInt(9)+1;
System.out.print("\nGuess the Number [1..10]\n");
System.out.print("===================\n");
System.out.print("You have " + lifeLeft + " chances.\n");
do {
do {
System.out.print("What number do I have in mind: ");
numGuess = scan.nextInt();
if (numGuess < 1 || numGuess > 10)
System.out.println("Invalid input. Range is 1-10.");
} while (numGuess < 1 || numGuess > 10);
if (numGuess != numRandom && lifeLeft != 0)
System.out.println("Wrong! You only have " + --lifeLeft + " chances left.");
} while (numGuess!=numRandom && lifeLeft > 0);
if (numGuess == numRandom)
System.out.println("Correct! -- in " + (life - lifeLeft) + " guess(es).");
if (lifeLeft == 0) {
System.out.println("You have no more lives..");
System.out.println("This is the number: " + numRandom);
}
System.out.print("\nEnter 'y' if you want to play again or any other character to exit: ");
want = scan.nextLine();
} while (want.equals("y") || want.equals("Y"));
}
public static void main(String[] args) {
new GuessNum();
}
}
推荐答案
使用want = scan.next();
代替nextLine()
.
出现问题的原因是,在前面的nextInt()
之后,您仍然在同一行上,而nextLine()
返回当前行的其余部分.
The reason for your problem is that following the preceding nextInt()
, you're still on the same line, and nextLine()
returns the rest of the current line.
这是重现此行为的最小代码段:
Here's a smallest snippet to reproduce the behavior:
Scanner sc = new Scanner(System.in);
System.out.println("nextInt() = " + sc.nextInt());
System.out.println("nextLine() = " + sc.nextLine());
输入时,例如说5
,然后按 Enter ,输出为:
When you type in, say, 5
and then hit Enter, the output is:
nextInt() = 5
nextLine() =
也就是说,nextLine()
不会阻止您的输入,因为当前行仍然剩余一个空字符串.
That is, nextLine()
did not block for your input, because the current line still has an empty string remaining.
为进行比较,当您键入内容时,说出5 yeah!
然后按 Enter ,则输出为:
For comparison, when you type in, say 5 yeah!
and then hit Enter, then the output is:
nextInt() = 5
nextLine() = yeah!
请注意," yeah!"
实际上与5
来自同一行.这与文档中指定的完全相同:
Note that " yeah!"
actually comes from the same line as the 5
. This is exactly as specified in the documentation:
String nextLine()
: Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line.
在半开范围内
假设要猜测的数字在1到10之间(含1和10),则以下代码为错误":
On half-open ranges
Assuming that the number to guess is between 1 and 10 inclusive, the following code is "wrong":
numRandom = rand.nextInt(9)+1; // this can only be in 1..9 range inclusive!
这是java.util.Random
文档的摘录:
int nextInt(int n)
: Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)
也就是说,就像Java API中的许多方法一样,Random.nextInt(int)
使用半开范围,包括下限和上限.
That is, like a lot of methods in Java's API, Random.nextInt(int)
uses the half-open range, with inclusive lower bound and exclusive upper bound.
这篇关于Java中的扫描仪无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!