代码执行没有明显的原因

时间:2017-09-26 10:32:00

标签: java java.util.scanner

我正在做一个小程序,用户可以选择要执行的选项。我使用switch来做,一切正常,但现在我正在尝试添加选择语言的能力,我正在尝试使用数组来完成这个(我不知道如何正确解释它,但它很容易见代码)。

//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

1 个答案:

答案 0 :(得分: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());