如果用户输入为空,是否有办法抛出错误消息?由于布尔变量,我无法使用catch(NumberFormatException e)。
boolean numberSet;
do {
numberSet = jerseys[position].setNumber(Integer.parseInt(JOptionPane.showInputDialog("Enter number of jersey")));
if (!numberSet) {
JOptionPane.showMessageDialog(null, "Number cannot be less than 0.");
}
} while (!numberSet);
setNumber方法
public boolean setNumber(int number) {
if (number >= 0) {
this.number = number;
return true;
}
else {
return false;
}
}