0
我正在做一個小程序,用戶選擇哪個選項想要做的。我使用開關做,並且一切正常,但現在我試圖增加選擇語言的能力,我試圖用數組來完成這個(我不知道如何正確解釋它,但是很容易請參閱代碼)。代碼執行沒有全部原因
//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次。這不是一個大麻煩,但我想解決它。
best ... title ... ever ...通常它會執行,因爲你運行它:)試圖在catch塊中設置一個不同的值? – Stultuske
@Stultuske是的,我試着在catch塊中設置_selection_的值。它沒有改變輸出。 –