我正在尝试声明当您输入整数时终止。我只能制作一个以整数继续的。我也在考虑尝试捕捉特定的错误,NumberFormatExeption
除了我不够好,无法弄清楚这是我的代码:
import javax.swing.JOptionPane;
import java.lang.NumberFormatException;
public class Calc_Test {
public static void main(String[] args) throws NumberFormatException{
while(true){
String INT= JOptionPane.showInputDialog("Enter a number here: ");
int Int = Integer.parseInt(INT);
JOptionPane.showConfirmDialog(null, Int);
break;
}
}
}
[编辑] 我清理了一些代码,并在朋友的帮助下解决了堆栈溢出问题。这是代码:
import javax.swing.JOptionPane;
public class Calc_Test {
public static void main(String[] args){
while(true){
String inputInt= JOptionPane.showInputDialog("Enter a number here: ");
if(inputInt.matches("-?\\d+")){
JOptionPane.showConfirmDialog(null, "\"" + inputInt + "\"" + " is a number");
break;
}
JOptionPane.showConfirmDialog(null, "\"" + inputInt + "\"" + " is not a number. Therefore, " + "\"" + inputInt + "\"" + " could not be parsed. Try again.");
}
}
}