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_的值。它没有改变输出。 –