我把我的 try catch 放在一个 do while 循环中,但是 do-while 循环之后的命令继续 运行 即使有一个异常被捕获
I put my try catch in a do while loop, but the commands after the do-while loop continue running even though there is an exception caught
我正在构建一个小程序来检查用户输入的内容是否为数字。程序运行,但是当我的 catch 块捕获异常时,它以某种方式退出了它所在的嵌套 do-while 循环。
这是我的程序:
package TESTCLASS;
import java.util.Scanner;
public class Apples {
static int readValidInt(Scanner in, String prompt, int min, int max){
int validUserInput;
int userInput = 0; //have to initialize this variable since I will be using it in a block, setting it to 0 for now
int checker =1;
while(!in.hasNextInt()) { //Makes sure that user inputs an Integer, not String, double, etc
System.out.println("Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4");
in.next();
}
do {
do {
try {
userInput = in.nextInt();
checker = 0;
}
catch (Exception e) {
System.out.println("Exception detectedddd");
in.nextLine(); // This line is to *clear the buffer* for Scanner
}
}while (checker ==1 );
if ( userInput >= min && userInput <= max) {
System.out.println("you have chosen board " + userInput );
validUserInput = userInput;
}
else {
System.out.println(prompt);
validUserInput = 0;
}
}while (validUserInput==0);
return validUserInput;
}
// Main function
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Choose a board style");
readValidInt(input, "Bruh that's not valid", 1, 4);
}
}
这是我的输出(如您所见,当我输入“五”时,会打印出两件事 - “Exception detectedddd”和“Bruh that's not valid”,但后一句是 if else 的一部分声明,由于存在异常,因此不应达到该声明:
选择棋盘样式
100
呃,那是无效的
五个
检测到异常
呃,那是无效的
六个
检测到异常
呃,那是无效的
10
呃,那是无效的
1
您选择了板1
您在第一次输入 (100) 后设置 checker = 0;
。由于该值太大,您打印“Bruh that's not valid”并留在外循环中。
这意味着您读取了另一个导致异常的值(5)。但是,由于 checker
已经是 0(从第一遍开始),因此不会重复内部循环。
您需要在每次开始内循环之前将checker
重置为1:
do {
checker = 1;
do {
try {
userInput = in.nextInt();
checker = 0;
}
catch (Exception e) {
System.out.println("Exception detectedddd");
in.nextLine(); // This line is to *clear the buffer* for Scanner
}
} while (checker == 1);
//... remainder of the outer loop
} while (validUserInput == 0);
我正在构建一个小程序来检查用户输入的内容是否为数字。程序运行,但是当我的 catch 块捕获异常时,它以某种方式退出了它所在的嵌套 do-while 循环。
这是我的程序:
package TESTCLASS;
import java.util.Scanner;
public class Apples {
static int readValidInt(Scanner in, String prompt, int min, int max){
int validUserInput;
int userInput = 0; //have to initialize this variable since I will be using it in a block, setting it to 0 for now
int checker =1;
while(!in.hasNextInt()) { //Makes sure that user inputs an Integer, not String, double, etc
System.out.println("Sorry, only numbers in integer form is allowed. Please enter your choice as an integer between 1 and 4");
in.next();
}
do {
do {
try {
userInput = in.nextInt();
checker = 0;
}
catch (Exception e) {
System.out.println("Exception detectedddd");
in.nextLine(); // This line is to *clear the buffer* for Scanner
}
}while (checker ==1 );
if ( userInput >= min && userInput <= max) {
System.out.println("you have chosen board " + userInput );
validUserInput = userInput;
}
else {
System.out.println(prompt);
validUserInput = 0;
}
}while (validUserInput==0);
return validUserInput;
}
// Main function
public static void main (String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Choose a board style");
readValidInt(input, "Bruh that's not valid", 1, 4);
}
}
这是我的输出(如您所见,当我输入“五”时,会打印出两件事 - “Exception detectedddd”和“Bruh that's not valid”,但后一句是 if else 的一部分声明,由于存在异常,因此不应达到该声明:
选择棋盘样式
100
呃,那是无效的
五个
检测到异常
呃,那是无效的
六个
检测到异常
呃,那是无效的
10
呃,那是无效的
1
您选择了板1
您在第一次输入 (100) 后设置 checker = 0;
。由于该值太大,您打印“Bruh that's not valid”并留在外循环中。
这意味着您读取了另一个导致异常的值(5)。但是,由于 checker
已经是 0(从第一遍开始),因此不会重复内部循环。
您需要在每次开始内循环之前将checker
重置为1:
do {
checker = 1;
do {
try {
userInput = in.nextInt();
checker = 0;
}
catch (Exception e) {
System.out.println("Exception detectedddd");
in.nextLine(); // This line is to *clear the buffer* for Scanner
}
} while (checker == 1);
//... remainder of the outer loop
} while (validUserInput == 0);