整数を入力すると終了するというステートメントを作成しようとしています。整数で続くものしか作れません。私はまた、特定のエラー魔女をキャッチしようと考えていましたが、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.");
}
}
}