使用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: ");
String sentence = scanner.nextLine();
System.out.print("Enter an index: ");
int index = scanner.nextInt();
System.out.println("
Your sentence: " + sentence);
System.out.println("Your index: " + index);
}
}
示例 1: 此示例按预期工作.行 String sentence = scanner.nextLine();
等待输入,然后继续 System.out.print("Enter an index: ");
>.
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: ");
.
这会产生输出:
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("
Menu Options
");
System.out.println("(1) - do this");
System.out.println("(2) - quit");
System.out.print("Please enter your selection: ");
int selection = scanner.nextInt();
if (selection == 1) {
System.out.print("Enter a sentence: ");
String sentence = scanner.nextLine();
System.out.print("Enter an index: ");
int index = scanner.nextInt();
System.out.println("
Your sentence: " + sentence);
System.out.println("Your index: " + index);
}
else if (selection == 2) {
break;
}
}
}
}
示例 2: 此示例未按预期工作.此示例使用 while 循环和 if - else 结构来允许用户选择要执行的操作.一旦程序到达String sentence = scanner.nextLine();
,它不会等待输入,而是执行System.out.print("Enter an index: ");
.
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: ");
.
这会产生输出:
Menu Options
(1) - do this
(2) - quit
Please enter your selection: 1
Enter a sentence: Enter an index:
这使得无法输入句子.
为什么示例 #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屋!