While - try - catch in Java
While - try - catch in Java
我需要一个 java 程序来询问 0 到 2 之间的数字。如果用户写入 0,程序将结束。如果用户写 1,它执行一个函数。如果用户写入 2,它会执行另一个函数。我还想用一条消息处理错误 "java.lang.NumberFormatException",在这种情况下,再次询问用户一个数字,直到他输入一个介于 0 和 2
之间的数字
我用
public static void main(String[] args) throws IOException {
int number = 0;
boolean numberCorrect = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (numberCorrect == false){
System.out.println("Choose a number between 0 and 2");
String option = br.readLine();
number = Integer.parseInt(option);
try {
switch(option) {
case "0":
System.out.println("Program ends");
numberCorrect = true;
break;
case "1":
System.out.println("You choose "+option);
functionA();
numberCorrect = true;
break;
case "2":
System.out.println("You choose "+option);
functionB();
numberCorrect = true;
break;
default:
System.out.println("Incorrect option");
System.out.println("Try with a correct number");
numberCorrect = false;
}
}catch(NumberFormatException z) {
System.out.println("Try with a correct number");
numberCorrect = false;
}
}
}
但是使用这段代码,catch(NumberFormatException z) 不起作用,程序不会再次询问号码。
您可以像这样将 try/catch 放在 parseInt 周围:
while (numberCorrect == false){
System.out.println("Choose a number between 0 and 2");
String option = br.readLine();
try {
number = Integer.parseInt(option);
}catch(NumberFormatException z) {
System.out.println("Try with a correct number");
numberCorrect = false;
option = "-1";
}
switch(option) {
case "0":
System.out.println("Program ends");
numberCorrect = true;
break;
...
你永远不会在这里真正捕捉到 NumberFormatException
。您的代码基本上是:
while (...) {
// this can throw NumberFormatException
Integer.parseInt(...)
try {
// the code in here cannot
} catch (NumberFormatException e) {
// therefore this is never reached
}
}
您想在这里做的是:
while (!numberCorrect) {
line = br.readLine();
try {
number = Integer.parseInt(line);
} catch (NumberFormatException ignored) {
continue;
}
// etc
}
我需要一个 java 程序来询问 0 到 2 之间的数字。如果用户写入 0,程序将结束。如果用户写 1,它执行一个函数。如果用户写入 2,它会执行另一个函数。我还想用一条消息处理错误 "java.lang.NumberFormatException",在这种情况下,再次询问用户一个数字,直到他输入一个介于 0 和 2
之间的数字我用
public static void main(String[] args) throws IOException {
int number = 0;
boolean numberCorrect = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
while (numberCorrect == false){
System.out.println("Choose a number between 0 and 2");
String option = br.readLine();
number = Integer.parseInt(option);
try {
switch(option) {
case "0":
System.out.println("Program ends");
numberCorrect = true;
break;
case "1":
System.out.println("You choose "+option);
functionA();
numberCorrect = true;
break;
case "2":
System.out.println("You choose "+option);
functionB();
numberCorrect = true;
break;
default:
System.out.println("Incorrect option");
System.out.println("Try with a correct number");
numberCorrect = false;
}
}catch(NumberFormatException z) {
System.out.println("Try with a correct number");
numberCorrect = false;
}
}
}
但是使用这段代码,catch(NumberFormatException z) 不起作用,程序不会再次询问号码。
您可以像这样将 try/catch 放在 parseInt 周围:
while (numberCorrect == false){
System.out.println("Choose a number between 0 and 2");
String option = br.readLine();
try {
number = Integer.parseInt(option);
}catch(NumberFormatException z) {
System.out.println("Try with a correct number");
numberCorrect = false;
option = "-1";
}
switch(option) {
case "0":
System.out.println("Program ends");
numberCorrect = true;
break;
...
你永远不会在这里真正捕捉到 NumberFormatException
。您的代码基本上是:
while (...) {
// this can throw NumberFormatException
Integer.parseInt(...)
try {
// the code in here cannot
} catch (NumberFormatException e) {
// therefore this is never reached
}
}
您想在这里做的是:
while (!numberCorrect) {
line = br.readLine();
try {
number = Integer.parseInt(line);
} catch (NumberFormatException ignored) {
continue;
}
// etc
}