私は、ユーザーがどちらのオプションを選択するかを選択する小さなプログラムを実行しています。私はスイッチを使用してそれを行い、すべて正常に動作しますが、今は言語を選択する機能を追加しようとしていますが、これを配列を使って実現しようとしています(正しく説明する方法はわかりませんが、コードを参照してください)。コードは親の理由なしに実行します
//Each menu option
static String[][] menuOptions =
{
{
"1. Check your grades",
"2. Check if you can examine for a driving license",
"3. Check if a number is odd or even",
"4. Check if a number is divisible by 7",
"5. Check if a triangle is equilater",
"6. Check who won",
"7. Check which number is bigger",
"8. Check if you can have a \"Carnet Jove\"",
"9. Convert numeric grades to letters",
"10. Exit Application"
},
{
"1. Comprobar si has aprobado",
"2. Check if you can examine for a driving license",
"3. Check if a number is odd or even",
"4. Check if a number is divisible by 7",
"5. Check if a triangle is equilater",
"6. Check who won",
"7. Check which number is bigger",
"8. Check if you can have a \"Carnet Jove\"",
"9. Convert numeric grades to letters",
"10. Exit Application"
}
};
//End of Options
static Scanner Scan = new Scanner(System.in);
static boolean correctInput = false;
static int language;
static int selection;
public static void main(String[] args)
{
System.out.println("\t1. English\t2. Español\n");
System.out.print("Select a Language:\tSeleccione un Idioma:\t");
language = Scan.nextInt();
System.out.print("");
while (correctInput != true)
{
menu(language);
try //Comprobamos que el usuario haya introducido un numero
{
selection = Integer.parseInt(Scan.nextLine());
} catch (NumberFormatException ex) //En caso de error lo gestionamos
{
//No hacemos nada
}
switch (selection)
{
case 1:
correctInput = true;
checkGrades();
break;
case 2:
correctInput = true;
chechDrivingLicense();
break;
case 3:
correctInput = true;
checkOddNum();
break;
case 4:
correctInput = true;
checkDivBy7();
break;
case 5:
correctInput = true;
checkEquilater();
break;
case 6:
correctInput = true;
checkWinner();
break;
case 7:
correctInput = true;
checkBigger();
break;
case 8:
correctInput = true;
checkCarnetJove();
break;
case 9:
correctInput = true;
convertNumGradeToLetter();
break;
case 10:
correctInput = true;
break;
default:
System.out.println("\n\n\n\nInput not valid. Please enter"
+ " a valid number\n");
}
}
}
private static void menu(int language)
{
System.out.println("\n");
int sel = 12;
for (String s : menuOptions[language - 1])
{
System.out.println("\t" + s);
}
System.out.print("\nSelect an option: \t");
}
代わりの選択メニューを表示する(ためには、現在唯一の選択肢翻訳を持っていますが、それが動作するかどうかをチェックするために十分であること)メニューが表示されたら、何が起こっていることは、それが自動的にオプションを選択していること(これは常に無効なものです)、メニュー1の繰り返しをトリガします。それは大きな迷惑ではないが、私はそれを修正したい。
ベスト...タイトル...これまで実行していたために実行されます:)キャッチブロックに別の値を設定しようとしましたか? – Stultuske
@Stultuskeはい、私は_selection_の値をキャッチブロックに設定しようとしました。それは出力を変更しませんでした。 –