为什么 try and catch 不起作用?
Why isn't the try and catch working?
我正在尝试制作一个程序,告诉人们他们是否可以乘坐过山车。只要输入有效,它就会工作,但我希望它显示一条消息,说明如果用户输入其他内容则重试。我尝试使用 try and catch,但它仍然显示错误(NumberFormat 异常)。感谢任何帮助,谢谢。
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
String strHeight, strBackTrouble, strHeartTrouble;
int intHeight;
strHeight = txtInputHeight.getText();
strBackTrouble = txtInputBackTrouble.getText();
strHeartTrouble = txtInputHeartTrouble.getText();
intHeight = Integer.parseInt(strHeight);
try {
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
} catch (Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}
}
intHeight = Integer.parseInt(strHeight);
抛出不在 catch 块中的异常。
try {
intHeight = Integer.parseInt(strHeight);
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
}
catch(NumberFormatException e) {
txtOutput.setText("You entered invalid number, please try again");
}
catch( Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}
java 7:
try {
intHeight = Integer.parseInt(strHeight);
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
} catch (NumberFormatException e | Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}
我正在尝试制作一个程序,告诉人们他们是否可以乘坐过山车。只要输入有效,它就会工作,但我希望它显示一条消息,说明如果用户输入其他内容则重试。我尝试使用 try and catch,但它仍然显示错误(NumberFormat 异常)。感谢任何帮助,谢谢。
private void btnCalculateActionPerformed(java.awt.event.ActionEvent evt) {
String strHeight, strBackTrouble, strHeartTrouble;
int intHeight;
strHeight = txtInputHeight.getText();
strBackTrouble = txtInputBackTrouble.getText();
strHeartTrouble = txtInputHeartTrouble.getText();
intHeight = Integer.parseInt(strHeight);
try {
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
} catch (Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}
}
intHeight = Integer.parseInt(strHeight);
抛出不在 catch 块中的异常。
try {
intHeight = Integer.parseInt(strHeight);
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
}
catch(NumberFormatException e) {
txtOutput.setText("You entered invalid number, please try again");
}
catch( Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}
java 7:
try {
intHeight = Integer.parseInt(strHeight);
if (intHeight < 188 || intHeight > 122) {
if (strBackTrouble.equals("N") || strBackTrouble.equals("n") || strHeartTrouble.equals("N") || strHeartTrouble.equals("n")) {
txtOutput.setText("It is OK for you to ride this roller coaster. Have fun!");
}
} else if (strBackTrouble.equals("Y") || strBackTrouble.equals("y") || strHeartTrouble.equals("Y") || strHeartTrouble.equals("y")) {
txtOutput.setText("Sorry, it is not safe for you to ride this rollercoaster");
}
} catch (NumberFormatException e | Exception e) {
txtOutput.setText("You entered invalid input, please try again");
}