- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用Scanner
方法nextInt()
和nextLine()
读取输入。
看起来像这样:
System.out.println("Enter numerical value");
int option;
option = input.nextInt(); // Read numerical value from input
System.out.println("Enter 1st string");
String string1 = input.nextLine(); // Read 1st string (this is skipped)
System.out.println("Enter 2nd string");
String string2 = input.nextLine(); // Read 2nd string (this appears right after reading numerical value)
input.nextLine()
被跳过,第二个
input.nextLine()
被执行,因此我的输出如下所示:
Enter numerical value
3 // This is my input
Enter 1st string // The program is supposed to stop here and wait for my input, but is skipped
Enter 2nd string // ...and this line is executed and waits for my input
input.nextInt()
。如果删除它,那么
string1 = input.nextLine()
和
string2 = input.nextLine()
都将按照我希望的方式执行。
最佳答案
这是因为Scanner.nextInt
方法不会在您按“ Enter”键创建的输入中读取换行符,因此对Scanner.nextLine
的调用在读取该换行符后返回。
在Scanner.nextLine
或任何Scanner.next()
方法(Scanner.nextFoo
本身除外)之后使用nextLine
时,您将遇到类似的行为。
解决方法:
在每个Scanner.nextLine
或Scanner.nextInt
之后放置一个Scanner.nextFoo
调用以消耗该行的其余部分,包括换行符
int option = input.nextInt();
input.nextLine(); // Consume newline left-over
String str1 = input.nextLine();
Scanner.nextLine
读取输入并将输入转换为所需的正确格式。例如,您可以使用
Integer.parseInt(String)
方法转换为整数。
int option = 0;
try {
option = Integer.parseInt(input.nextLine());
} catch (NumberFormatException e) {
e.printStackTrace();
}
String str1 = input.nextLine();
关于java - 扫描程序在使用next()或nextFoo()之后跳过nextLine()吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60705055/
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我使用 Scanner 方法 nextInt() 和 nextLine() 来读取输入。 看起来像这样: System.out.println("Enter numerical value");
我是一名优秀的程序员,十分优秀!