- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我在尝试使用 java.util.Scanner 中的 nextLine() 方法时遇到了问题。
这是我尝试过的:
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: 此示例按预期工作。 String sentence =scanner.nextLine();
行等待输入输入,然后继续执行 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("Enter an index:\t");
.
这会产生输出:
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() 的行为。
最佳答案
我认为你的问题是
int selection = scanner.nextInt();
只读取数字,而不是行尾或数字之后的任何内容。当你声明
String sentence = scanner.nextLine();
这会读取带有数字的行的其余部分(我怀疑的数字后面没有任何内容)
尝试放置一个scanner.nextLine();如果您打算忽略该行的其余部分,则在每个 nextInt() 之后。
关于java - 使用scanner.nextLine(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5032356/
scanner.Scanner 之间有什么区别?来自包裹text/scanner ,和一个 bufio . Scanner ? 最佳答案 text/scanner更适合阅读源代码,主要是 Go 源代码
我有以下代码: Scanner in = new Scanner (System.in); String[] data = new String[5]; System.out.println("Ple
我有 Java 代码,它要求用户输入,然后将此数据存储在字符串变量中。下面的函数是“number”类的一部分,并在主函数中调用。 public static void setVal(i
我正在编写一个java程序,它运行一个循环并不断询问用户输入。然后程序用该字符串执行一系列操作,并请求另一个字符串并重复。 问题是许多字符串非常相似,所以我想用循环中上次的输入填充提示。例如:如果用户
我在 O'Reillys Java Cookbook(第 2 版)中寻找一些好东西,发现 Scanner.create() 方法大约 10 次。但是在 API 或类声明\实现中没有这样的东西。例如:P
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个答案) 关闭 4 年前。 im
这样做有什么好处/坏处吗? 通常,从流中读取时会抛出异常: try { inputStream.read(); }catch(IOException e) { e.printStack
Scanner console=new Scanner(System.in); System.out.print("how many:"); int n=console.nextInt(); cons
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 6 年前。 我最近刚刚
Scanner input = new Scanner(System.in); 你能详细解释一下上面的代码一步一步做了什么吗?我真的不明白它是如何工作的以及它如何链接到我以后能够做这个声明: int
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (24 个答案) 关闭 6 年前。 我必
如果我在 java.util 上调用 scanner.hasNext(pattern),然后使用相同的模式调用 scanner.next(pattern),我会扫描两次吗?扫描仪 假设我有很多案例的这
这是我的问题: 我正在尝试使用 Scanner 和 System.in 从键盘获取输入并将其分配给 int 变量。 这就是我所拥有的(完整的程序如下): // this program will us
使用 try(Scanner scan = new Scanner(System.in)) { } 导致 Exception in thread "main" java.util.NoSuchElem
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
此问题仅用于教育目的。我从 Java 教科书中获取了以下代码,我很好奇为什么在 catch block 中使用了 input.nextLine()。 我尝试使用 input.nextInt() 代替它
这个问题已经有答案了: How to use java.util.Scanner to correctly read user input from System.in and act on it?
我的教授倾向于执行以下操作以从用户那里获取数字: Scanner scanner = new Scanner(System.in); Integer.parseInt(scanner.nextLine
我在使用 Scanner 时出现奇怪的行为。当我使用 Scanner(FileInputStream) 构造函数时,它将与我正在使用的一组特定文件一起使用,但它不适用于 Scanner(File) 构
D 中是否有类似于 Java 扫描仪的流解析器?您可以去哪里nextInt()获取 int和 nextLong()对于 long , 等等。 最佳答案 std.conv.parse 类似: http:
我是一名优秀的程序员,十分优秀!