我正在尝试制作一个程序,告诉他们是否可以乘坐过山车.只要输入有效,它就可以工作,但我希望它显示一条消息,说明如果用户输入其他内容则重试.我尝试使用try和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");
}
}
Run Code Online (Sandbox Code Playgroud)
intHeight = Integer.parseInt(strHeight);
Run Code Online (Sandbox Code Playgroud)
抛出不在catch块中的Exception.