2017-09-26 95 views
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次。这不是一个大麻烦,但我想解决它。

这里有一个电流输出的样子: enter image description here

+4

best ... title ... ever ...通常它会执行,因为你运行它:)试图在catch块中设置一个不同的值? – Stultuske

+0

@Stultuske是的,我试着在catch块中设置_selection_的值。它没有改变输出。 –

回答

3
language = Scan.nextInt(); 

这读取一个整数,而不是后面的换行符。

selection = Integer.parseInt(Scan.nextLine()); 

第一次到达时,有一个换行符正在等待阅读。 nextLine()立即返回一个空字符串。

catch (NumberFormatException ex) //En caso de error lo gestionamos 
{ 
    //No hacemos nada 
} 

如果您没有吞下产生的异常,您可能会看到这一点。不要无所事事地处理异常!至少请致电ex.printStackTrace()查看错误消息。更好的是,请用户再试一次。

要修复您的程序,请避免混合使用nextInt()nextLine()。总是使用nextLine()更好。阅读language就像你做selection一样,你会变得更好。

language = Integer.parseInt(Scan.nextLine());