try-and-catch JAVA的无止境循环
所以我要做的是让用户在矩阵中输入一个有效的坐标,这是一个大于-1的整数try-and-catch JAVA的无止境循环,java,loops,while-loop,try-catch,Java,Loops,While Loop,Try Catch,所以我要做的是让用户在矩阵中输入一个有效的坐标,这是一个大于-1的整数 Scanner scanner = new Scanner (System.in); int coordinates[] = new int[2]; coordinates[0]=-1; coordinates[1]=-1; boolean check = true; while (((coordinates[0]<0)||(coordinates[0]>R)) && check) {
Scanner scanner = new Scanner (System.in);
int coordinates[] = new int[2];
coordinates[0]=-1;
coordinates[1]=-1;
boolean check = true;
while (((coordinates[0]<0)||(coordinates[0]>R)) && check) {
System.out.print("Please enter a valid row number:\t");
try {
coordinates[0]=scanner.nextInt();
break;
}
catch (InputMismatchException e) {
}
}
while (((coordinates[1]<0)||(coordinates[1]>C)) && check) {
System.out.print("Please enter a valid col number:\t");
try {
coordinates[1]=scanner.nextInt();
break;
}
catch (InputMismatchException e) {
}
}
Scanner Scanner=新的扫描仪(System.in);
整数坐标[]=新整数[2];
坐标[0]=-1;
坐标[1]=-1;
布尔检查=真;
while(((坐标[0]R))&&check){
System.out.print(“请输入有效的行号:\t”);
试试{
坐标[0]=scanner.nextInt();
打破
}
捕获(输入不匹配异常e){
}
}
while(((坐标[1]C))&&check){
System.out.print(“请输入有效的列号:\t”);
试试{
坐标[1]=scanner.nextInt();
打破
}
捕获(输入不匹配异常e){
}
}
问题是它在输入无效输入后会无休止地循环
int R是行的大小
int C是COLLMN的大小如果“无效输入”的意思是“不是任何类型的整数”,那么您的扫描器每次尝试读取另一个整数时都会失败,因此您将点击您的捕捉
,并且不会停止循环。在这种情况下,您可能打算将check
设置为false
?或者,您是想在每个捕获
中放置中断
在读取有效整数时使用中断是不正确的,因为它可能是一个负整数,而您的循环保护程序会说您不想要它。您的问题是您没有处理捕获到的错误。 如果为
nextInt()
方法提供错误的数字格式,则将抛出InputMismatchException
。然后,由于捕获不做任何操作,循环将继续(从开始),扫描仪将读取相同的错误值,以此类推
因此,与此相反:
catch (InputMismatchException e) {
}
试试这个:
catch (InputMismatchException e) {
System.out.println("Wrong number entered.");
scanner.nextLine();
}
这样,您将强制扫描仪
越过最后一个不正确的输入
编辑:
public static void main(final String args[])
{
int maxRowsNumber = 10;
int maxColsNumber = 10;
Scanner scanner = new Scanner (System.in);
int coordinates[] = new int[2];
coordinates[0]=-1;
coordinates[1]=-1;
boolean check = true;
while (((coordinates[0]<0)||(coordinates[0]>maxRowsNumber)) && check) {
System.out.print("Please enter a valid row number:\t");
try {
coordinates[0]=scanner.nextInt();
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
scanner.nextLine();
}
}
while (((coordinates[1]<0)||(coordinates[1]>maxColsNumber)) && check) {
System.out.print("Please enter a valid col number:\t");
try {
coordinates[1]=scanner.nextInt();
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
scanner.nextLine();
}
}
System.out.println("Inserted RowsNumber: " + coordinates[0]);
System.out.println("Inserted RowsNumber: " + coordinates[1]);
}
由于读取输入后执行了break
,因此循环也被中断。在这种情况下,如果您输入负数,您也将中断
,并且不会检查循环条件。删除break
语句,它将按预期工作:
while (((coordinates[0]<0)||(coordinates[0]>R)) && check) {
System.out.print("Please enter a valid row number:\t");
try {
coordinates[0]=scanner.nextInt();
}
catch (InputMismatchException e) {
System.out.println("That's not a valid number.");
scanner.nextLine();
}
}
这与您所做的基本相同,我只是尝试通过删除硬编码的值,使变量更具描述性,并包括输入验证来改进它
final int ROW = 0;
final int COL = 1;
int coordinates[] = new int[2];
coordinates[ROW] = -1;
coordinates[COL] = -1;
boolean isInputValid = true;
Scanner scanner = new Scanner(System.in);
do {
try {
System.out.print("Please enter a valid row number:\t");
coordinates[ROW] = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException nfe) {
isInputValid = false; //if the input is not int
}
} while (!isInputValid && (coordinates[ROW] < 0) //do this until the input is an int
|| (coordinates[ROW] > R)); //and it's also not less than 0 or greater than R
//same logic applies here
do {
try {
System.out.print("Please enter a valid col number:\t");
coordinates[COL] = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException nfe) {
isInputValid = false;
}
} while (!isInputValid && (coordinates[COL] < 0)
|| (coordinates[COL] > C));
final int ROW=0;
最终整数列=1;
整数坐标[]=新整数[2];
坐标[行]=-1;
坐标[COL]=-1;
布尔值isInputValid=true;
扫描仪=新的扫描仪(System.in);
做{
试一试{
System.out.print(“请输入有效的行号:\t”);
坐标[ROW]=Integer.parseInt(scanner.nextLine());
}捕获(NumberFormatException nfe){
isInputValid=false;//如果输入不是int
}
}while(!isInputValid&&(坐标[ROW]<0)//执行此操作,直到输入为int
||(坐标[ROW]>R)//它也不小于0或大于R
//同样的逻辑也适用于此
做{
试一试{
System.out.print(“请输入有效的列号:\t”);
坐标[COL]=Integer.parseInt(scanner.nextLine());
}捕获(NumberFormatException nfe){
isInputValid=false;
}
}而(!isInputValid&&(坐标[COL]<0)
||(坐标[COL]>C);
希望这能有所帮助。这就是为什么我要暂停?这样,我想,一旦存储完成,程序就会退出while。为什么您甚至有检查标志?还有,什么是R&C?您可能希望清理此代码,示例中不需要第二个循环,R/C丢失了先生,我遵循了您的建议,现在我的问题是它接受负整数,您能帮我吗,先生?当然可以,但首先您必须回答评论中的问题-R&C是什么?int R是行的大小int C是列的大小,R和C将由用户输入,这将是其所需尺寸matrix@dumas-编辑了我的asnswer,我希望它对你有用。如果是这样的话,你接受答案。现在我去掉了中断;它不断要求用户输入一个数字,即使它是有效的,先生,谢谢您的帮助和耐心。
final int ROW = 0;
final int COL = 1;
int coordinates[] = new int[2];
coordinates[ROW] = -1;
coordinates[COL] = -1;
boolean isInputValid = true;
Scanner scanner = new Scanner(System.in);
do {
try {
System.out.print("Please enter a valid row number:\t");
coordinates[ROW] = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException nfe) {
isInputValid = false; //if the input is not int
}
} while (!isInputValid && (coordinates[ROW] < 0) //do this until the input is an int
|| (coordinates[ROW] > R)); //and it's also not less than 0 or greater than R
//same logic applies here
do {
try {
System.out.print("Please enter a valid col number:\t");
coordinates[COL] = Integer.parseInt(scanner.nextLine());
} catch (NumberFormatException nfe) {
isInputValid = false;
}
} while (!isInputValid && (coordinates[COL] < 0)
|| (coordinates[COL] > C));