【发布时间】:2014-10-17 18:12:19
【问题描述】:
我有点不确定这是否:
try{
worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
try{
worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
会做同样的事情:
try{
worldHeight = Integer.parseInt(JOptionPane.showInputDialog("How many cells high will the world be?: "));
worldWidth = Integer.parseInt(JOptionPane.showInputDialog("How many cells wide will the world be?: "));
}
catch (NumberFormatException e){
JOptionPane.showMessageDialog(null, "You have not entered a valid number");
}
基本上希望用户输入一个数字,如果不是数字则抛出异常并重新要求用户输入一个数字?
谢谢
【问题讨论】:
标签: java exception optimization try-catch joptionpane