我在下面编写的程序是这样设计的,如果用户在提示时没有输入整数,程序将循环直到它们为止。这适用于初始检查,但第二次检查不起作用。代码如下:
import java.util.Scanner;
public class SafeInput
{
public static void main(String[]args)
{
System.out.println("Please enter any integer. If you want to exit, enter -1>");
Scanner scan = new Scanner(System.in);
while(!scan.hasNextInt())
{
String garbage = scan.nextLine();
System.out.println("You've entered garbage.");
}
int input = scan.nextInt();
while(input != -1)
{
System.out.println("\nYou entered: "+input);
System.out.println("Please enter any integer. If you want to exit, enter -1>");
while(!scan.hasNextInt())
{
System.out.println("You've entered garbage.");
String garbage1 = scan.nextLine();
}
input = scan.nextInt();
}
System.out.println("-1 entered. Goodbye");
}
}
以下是我执行程序时会发生的事情:
Please enter any integer. If you want to exit, enter -1>
this is a string
You've entered garbage.
1
You entered: 1
Please enter any integer. If you want to exit, enter -1>
2
You entered: 2
Please enter any integer. If you want to exit, enter -1>
this is also a string
You've entered garbage.
You've entered garbage.
string
You've entered garbage.
1
You entered: 1
Please enter any integer. If you want to exit, enter -1>
2
You entered: 2
Please enter any integer. If you want to exit, enter -1>
-1
-1 entered. Goodbye
为什么当我第二次检查整数失败时,程序输出:
You've entered garbage.
You've entered garbage.
而不是:
You've entered garbage.
谢谢!
答案 0 :(得分:0)
第二个while循环应该如下更新。
while(input != -1)
{
System.out.println("\nYou entered: "+input);
System.out.println("Please enter any integer. If you want to exit, enter -1>");
scan.nextLine();
while(!scan.hasNextInt())
{
System.out.println("You've entered garbage");
String garbage1 = scan.nextLine();
}
input = scan.nextInt();
}
答案 1 :(得分:0)
使用next()而不是nextLine()。
while(input != -1) {
System.out.println("\nYou entered: "+input);
System.out.println("Please enter any integer. If you want to exit, enter -1>");
while(!scan.hasNextInt())
{
System.out.println("You've entered garbage.");
String garbage1 = scan.next();
}
input = scan.nextInt();
}
查看Scanner#nextLine()java doc,
此方法返回当前行的其余部分,不包括任何行 最后的分隔符。该位置设置为下一个的开头 线。
参见代码
调试:
while(input != -1)
{
System.out.println("\nYou entered: "+input);
System.out.println("Please enter any integer. If you want to exit, enter -1>");
while(!scan.hasNextInt())
{
System.out.println("-- Second -- ");
System.out.println("You've entered garbage.");
String garbage1 = scan.nextLine();
System.out.println(" garbage1 -> " + garbage1);
}
input = scan.nextInt();
}
输出:
Please enter any integer. If you want to exit, enter -1>
q
-- First --
You've entered garbage.
1
-- input --1
You entered: 1
Please enter any integer. If you want to exit, enter -1>
aa
-- Second --
You've entered garbage.
garbage1 ->
-- Second --
You've entered garbage.
garbage1 -> aa
ab
-- Second --
You've entered garbage.
garbage1 -> ab
为了好玩,我做了一些'增强'实施。它应该正常工作。
package example;
import java.util.Scanner;
public class SafeInput {
public static void main(String[] args) {
System.out.println("Please enter any integer. If you want to exit, enter -1>");
try (Scanner scan = new Scanner(System.in)) {
while (true) {
int input = 0;
String garbageOutput = null;
if (scan.hasNextInt() && ((input = scan.nextInt()) != -1)) {
System.out.println("\nYou entered: " + input);
System.out.println("Please enter any integer. If you want to exit, enter -1>");
} else if (scan.hasNextInt() && ((input = scan.nextInt()) == -1)) {
System.out.println("-1 entered. Goodbye");
break;
} else if (!scan.hasNextInt() && (scan.hasNextLine())
&& (!(garbageOutput = scan.nextLine()).isEmpty())) {
System.out.println(String.format("You've entered garbage - [%s]", garbageOutput));
}
}
}
}
}