問題があります。このコードでは、数字が異なる2桁の整数を取ります。それが整数でない場合、コードはあなたに指示し、再入力を求めます。それが整数であるが条件に合わない場合、それはあなたに伝えられ、再入力を促します。ループ/プログラムを終了するために0を入力する以外はすべて動作します。私は2日間これに固執してきた。助言がありますか?範囲と入力タイプのバリデーション
int num = 1;
while (num != 0) {
System.out.print("Enter a 2-digit number. The digits should be different. Zero to stop: ");
while (!in.hasNextInt()) {
System.out.print("Not an integer, try again: ");
in.next();
}
num = in.nextInt();
while (num < 10 || num > 99) {
System.out.println("NOT good for your game!");
System.out.print("Enter a 2-digit number. The digits should be different. Zero to stop: ");
while (!in.hasNextInt()) {
System.out.print("Not an integer, try again: ");
in.next();
}
num = in.nextInt();
}
if (equalDigs(num) == false)
System.out.println("NOT good for you game!");
else
System.out.println("Good for your game! Play!");
}
}
public static boolean equalDigs(int n) {
int d1 = n/10;
int d2 = n % 10;
if (d1 == d2)
return false;
else
return true;
}
この使用例では、do-whileを使用してください。試してみると、問題が非常に単純化されます。 –