当我输入一个字符时,下面的代码片段会导致无限循环。这对我来说真的很奇怪,因为错误的数字可以正常工作,但是当我输入任何字符时,它会导致无限循环。
boolean checkValue = false;
Scanner console = new Scanner(System.in);
while (!checkValue) {
System.out.println("Enter 1, 2 or 3");
try {
input = console.nextInt();
switch (input) {
case 1:
// code
checkValue = true;
break;
case 2:
// code
checkValue = true;
break;
case 3:
// code
checkValue = true;
break;
default: // when entered a wrong number
System.err.println("Wrong Input");
checkValue = false;
}
}
catch(Exception e) { // when entered a character
System.err.println("Wrong Input");
checkValue = false;
}
}
答案 0 :(得分:1)
您将输入分配到一个变量中:
input = console.nextInt();
但是switch语句测试了一个不同的变量:
switch (auswahl) {
编辑:
将console.nextLine()
添加到您的异常处理程序中,以使用当前行的结尾,以允许下一行nextInt
从下一行读取。
答案 1 :(得分:1)
什么是“auswha1”?它的值不会更新,您的扫描仪不在循环中。 此外,您确定要在交换机中测试角色吗?它看起来像一个int。
编辑: 在调用nextInt之前实例化一个新的扫描程序,它应该工作:
try {
Scanner console = new Scanner(System.in);
int input = console.nextInt();
switch (input) {
...
答案 2 :(得分:1)
您需要检查输入的正确类型,而不是尝试捕获异常。当您发现提供了错误的输入时,您必须使用输入才能继续下一次尝试。像这样:
public static void main(String[] args) {
boolean checkValue = false;
int input;
Scanner console = new Scanner(System.in);
while (!checkValue) {
System.out.println("Enter 1, 2 or 3");
// Check if we have an integer
if (console.hasNextInt()) {
input = console.nextInt();
switch (input) {
case 1:
// code
checkValue = true;
break;
case 2:
// code
checkValue = true;
break;
case 3:
// code
checkValue = true;
break;
default: // when entered a wrong number
System.err.println("Wrong Input");
checkValue = false;
}
}
else {
// when entered a character
System.err.println("Wrong Input");
// Consume the wrong input.
console.next();
checkValue = false;
}
}
}
答案 3 :(得分:1)
由于您使用console.nextInt()
,因此您将只读取输入中的整数。然后,当您输入不是int的内容时,您会捕获异常。在那里你需要消费坏输入,例如使用nextLine()
:
catch(Exception e) { // when entered a character
System.err.println("Wrong Input");
checkValue = false;
console.nextLine(); // <=== add
}
然后它有用......
答案 4 :(得分:0)
auswahl变量未分配您的输入。实际上,这个变量甚至不在场景中,直到你在开关中使用它。
boolean checkValue = false;
int input;
Scanner console = new Scanner(System.in);
while (!checkValue) {
System.out.println("Enter 1, 2 or 3");
try {
input = console.nextInt();
switch (input) {
case 1:
// code
checkValue = true;
break;
case 2:
// code
checkValue = true;
break;
case 3:
// code
checkValue = true;
break;
default: // when entered a wrong number
System.err.println("Wrong Input");
checkValue = false;
}
}
catch(Exception e) { // when entered a character
System.err.println("Wrong Input");
checkValue = false;
}
System.out.println("Program Ends");
答案 5 :(得分:0)
boolean checkValue = true;
Scanner console = new Scanner(System.in);
while (checkValue) {
System.out.println("Enter 1, 2 or 3");
try {
input = console.nextInt();
switch (input) {
case 1:
// code
checkValue = false;
break;
case 2:
// code
checkValue = false;
break;
case 3:
// code
checkValue = false;
break;
default: // when entered a wrong number
System.err.println("Wrong Input");
checkValue = false;
}
}
catch(Exception e) { // when entered a character
System.err.println("Wrong Input");
checkValue = false;
}
}
答案 6 :(得分:0)
输入任何非法字符后,您可能会获得java.util.InputMismatchException
。它不会清除输入缓冲区,因此调用nextInt()会导致相同的错误等。
添加console.next(); to catch子句跳过错误的字符。
答案 7 :(得分:0)
这将有效
catch (Exception e) { // when entered a character
System.err.println("Wrong Input");
checkValue = false;
console = new Scanner(System.in);//Edit
}