我遇到了 nextLine
忽略用户输入的问题。经过一番研究,我发现我必须使用 in.nextLine()
清除 Scanner
,但是你能告诉我为什么吗?我想知道当我使用这个时我在做什么:
String edit = in.nextLine();
switch (edit) {
case "name":
System.out.println("Enter new name for " + items.get((editItem - 1)).getName());
String newName = in.nextLine();
items.get((editItem - 1)).setName(newName);
最佳答案
请访问以下链接以更好地理解为什么 nextline() 不读取输入:
Scanner is skipping nextLine() after using next() or nextFoo()?
关于java - nextLine 忽略输入,但 next() 不忽略 - 为什么我要清除扫描仪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54420387/