-1
System.out.println("Insert first number : ");
Scanner ad = new Scanner(System.in);
int y = ad.nextInt();
System.out.println("Insert Second Number: ");
Scanner er = new Scanner(System.in);
int x = er.nextInt();
int z;
z = x + y;
if (z > 10) {
System.out.println(z + " is greater than 10");
} else if (z < 9) {
System.out.println(z + " is less than 10");
} else {
System.out.println(z + " is equal to 10");
}
数字の代わりに文字を入力すると、各if文の出力をエラーにします。 入力した文字が無効であることを出力します。変数の入力が数値ではない場合の無効な結果(出力)の挿入方法
なぜ2つのスキャナインスタンスを使用するのですか? –
ユーザーからの値を受け入れるときに、開始時に値がintかどうかを確認する必要があります。 – Ragxion
[Java.util.scannerエラー処理]の重複の可能性あり(https://stackoverflow.com/questions/2696063/java-util-scanner-error-handling) – Stavr00