导致infite循环的异常处理(java)
您好,我是一名Java初学者。我想制作一个取1到9之间整数的方法。我正在使用异常处理程序,以便它可以处理错误或不匹配的输入,但它似乎只执行语句“choice=input.nextInt()”一次,因此我的循环变为无限 代码如下:导致infite循环的异常处理(java),java,exception-handling,user-input,Java,Exception Handling,User Input,您好,我是一名Java初学者。我想制作一个取1到9之间整数的方法。我正在使用异常处理程序,以便它可以处理错误或不匹配的输入,但它似乎只执行语句“choice=input.nextInt()”一次,因此我的循环变为无限 代码如下: import java.util.*; public class Player{ private int[] over; private int choice; private int coordinates[]; private st
import java.util.*;
public class Player{
private int[] over;
private int choice;
private int coordinates[];
private static Scanner input = new Scanner(System.in);
public Player(){
over = new int[5];
for(int i = 0; i < 5; i++){
over[i] = 1;
}
coordinates = new int[2];
coordinates[0] = coordinates[1] = -1;
}
public void getChoice(){
int choice = -1;
boolean inputIsOk;
do{
System.out.print("Enter Your Choice: ");
inputIsOk = true;
try{
choice = input.nextInt();
}
catch(InputMismatchException e){
System.out.println("Invalid choice");
inputIsOk = false;
}
if(choice < 1 || choice > 9){
System.out.println("Enter Choice In Range(1-9)");
inputIsOk = false;
}
}while(!inputIsOk);
System.out.println("You Entered "+choice);
}
}
以下是输出:
仅输入整数选项的第一种情况
harsh@harsh-Inspiron-3558:~/java/oxgame$ javac TestPlayer.java
harsh@harsh-Inspiron-3558:~/java/oxgame$ java TestPlayer
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: -1
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice: 5
You Entered 5
第二,输入错误时:
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice:g
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
and it goes on....
请帮帮我,谢谢。这会有用的
try{
choice = Integer.parseInt(input.next());
}
catch(NumberFormatException e){
System.out.println("Invalid choice");
inputIsOk = false;
}
理由是:
假设扫描仪从流中读取对象,比如说typeCache
。在它没有得到整数值之前,缓冲区不会被刷新,typeCache
将保持String
,直到使用next()
(或任何等效工具)读取它为止
扫描仪的代码
类别:
public int nextInt() {
return nextInt(defaultRadix);
}
public int nextInt(int radix) {
// Check cached result
if ((typeCache != null) && (typeCache instanceof Integer)
&& this.radix == radix) {
int val = ((Integer)typeCache).intValue();
useTypeCache();
return val;
}.......
或
只需添加
input.next()代码>在catch块中,它将自动清除typeCache
。如果您按以下方式更改catch子句:
} catch (InputMismatchException e) {
input.next();
System.out.println("Invalid choice");
inputIsOk = false;
}
这将起作用,input.next()我不知道确切的原因,
当您输入g时,旧代码只是执行这个choice=input.nextInt()代码>好像它仍然保持相同的值,它没有等待用户输入,调用next()
修复了这个问题。可能重复的Sir解释是精确的,但更技术,因为我是初学者,你能用更简单的方式解释吗?但是谢谢你的解决方案。简单地说:::假设有一个缓冲区,它保存来自输入蒸汽的数据。当我们从缓冲区读取数据时,它会被刷新以从流中接收新数据。通过使用nextInt(),它的作用是,如果缓冲区保存整数数据,则返回数据并清除缓冲区,否则扫描仪将等待缓冲区被读取。例如:立即写入typeCache=“g”
(如果输入g)。如果我们使用nextInt()
,它将检查typeCache
是否包含int。如果它包含int,那么该值将被返回,typeCache
将保持/重置为流中的下一个值。否则,typeCache
将包含“g”,直到未调用next()/nextLine()方法。非常感谢,您帮了很多忙。
} catch (InputMismatchException e) {
input.next();
System.out.println("Invalid choice");
inputIsOk = false;
}