使用scanner.nextLine() [英] Using scanner.nextLine()
问题描述
尝试使用java.util.Scanner中的nextLine()方法时遇到了麻烦。
I have been having trouble while attempting to use the nextLine() method from java.util.Scanner.
以下是我的尝试:
import java.util.Scanner;
class TestRevised {
public void menu() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
}
示例#1:此示例按预期工作。行字符串句子= scanner.nextLine();
在继续 System.out.print(输入索引之前等待输入输入:\t);
。
Example #1: This example works as intended. The line String sentence = scanner.nextLine();
waits for input to be entered before continuing on to System.out.print("Enter an index:\t");
.
这会产生输出:
Enter a sentence: Hello.
Enter an index: 0
Your sentence: Hello.
Your index: 0
// Example #2
import java.util.Scanner;
class Test {
public void menu() {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("\nMenu Options\n");
System.out.println("(1) - do this");
System.out.println("(2) - quit");
System.out.print("Please enter your selection:\t");
int selection = scanner.nextInt();
if (selection == 1) {
System.out.print("Enter a sentence:\t");
String sentence = scanner.nextLine();
System.out.print("Enter an index:\t");
int index = scanner.nextInt();
System.out.println("\nYour sentence:\t" + sentence);
System.out.println("Your index:\t" + index);
}
else if (selection == 2) {
break;
}
}
}
}
示例#2:此示例无法按预期工作。此示例使用while循环和if - else结构,以允许用户选择要执行的操作。程序到达 String sentence = scanner.nextLine();
后,它不会等待输入,而是执行 System.out.print行(输入索引:\t);
。
Example #2: This example does not work as intended. This example uses a while loop and and if - else structure to allow the user to choose what to do. Once the program gets to String sentence = scanner.nextLine();
, it does not wait for input but instead executes the line System.out.print("Enter an index:\t");
.
这会产生输出:
Menu Options
(1) - do this
(2) - quit
Please enter your selection: 1
Enter a sentence: Enter an index:
这使得无法输入一句话。
Which makes it impossible to enter a sentence.
为什么示例#2不按预期工作? Ex之间的唯一区别。 1和2是Ex。 2有一个while循环和一个if-else结构。我不明白为什么这会影响scanner.nextInt()的行为。
Why does example #2 not work as intended? The only difference between Ex. 1 and 2 is that Ex. 2 has a while loop and an if-else structure. I don't understand why this affects the behavior of scanner.nextInt().
推荐答案
我认为你的问题是
int selection = scanner.nextInt();
只读取数字,而不是数字后面的行尾或任何内容。当你声明
reads just the number, not the end of line or anything after the number. When you declare
String sentence = scanner.nextLine();
这将读取行上剩余的数字(我怀疑的数字后面没有任何内容)
This reads the remainder of the line with the number on it (with nothing after the number I suspect)
尝试放置scanner.nextLine();如果你打算忽略该行的其余部分,则在每个nextInt()之后。
Try placing a scanner.nextLine(); after each nextInt() if you intend to ignore the rest of the line.
这篇关于使用scanner.nextLine()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!