2017-09-26 8 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

ベスト...タイトル...これまで実行していたために実行されます:)キャッチブロックに別の値を設定しようとしましたか? – Stultuske

+0

@Stultuskeはい、私は_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()); 
関連する問題