是否可以使用二维数组创建一个迭代菜单的方法?

时间:2015-11-15 14:25:22

标签: java arrays loops

下面我创建了一个方法,在获取用户输入时使用do while循环遍历菜单。

/**
 * Method to iterate through a menu using a do while loop.
 */

public static void menuMethodDoWhile(){
    // create the scanner class
    Scanner menuScanner = new Scanner(System.in);

    // declare my variable and assign a value to it
    int menuOption = 0;

    // try catch finally blocks to handle errors
    try {
        // do while loop to print a menu to the screen and request input from the user
        do {
            // try catch blocks to handle user input errors
            try {
                // menu to be displayed
                System.out.println("Menu");
                System.out.println("1. File");
                System.out.println("2. Edit");
                System.out.println("3. Save");
                System.out.println("4. Delete");
                System.out.println("5. Exit");
                System.out.println("Select option.");
                // request input from the user
                menuOption = menuScanner.nextInt();
                // if else statements to determine the user's answer
                if (menuOption == 1){
                    System.out.println("You have selected File.");
                } else if (menuOption == 2){
                    System.out.println("You have selected Edit.");
                } else if (menuOption == 3){
                    System.out.println("You have selected Save.");
                } else if (menuOption == 4){
                    System.out.println("You have selected Delete.");
                } else if (menuOption == 5){
                    System.out.println("You have selected Exit.");
                }
            } catch (InputMismatchException inputMismatchException){
                // catch block
                menuScanner.nextLine();
                System.out.println("Numbers only please.");
            }
        } while (menuOption != 5);
    } catch (Exception exception){
        // catch block
        System.out.println("Something went wrong.");
    } finally {
        // finally block
        System.out.println("Exiting program.");
        menuScanner.close();
    }
}

我只是想知道是否可以通过将每个菜单选项分配给2D数组并使用嵌套for循环迭代它来缩短此代码,如果是这样,我将如何进行此操作。到目前为止,我所有的就是这个。

/**
 * Method to iterate through a menu using a 2D array and a nested for loop.
 */

public static void menuMethodArrayFor(){
    // create the scanner class
    Scanner menuScanner = new Scanner(System.in);

    // request input from the user
    System.out.println("Select option.");
    String menuOption = menuScanner.nextLine();

    // declare my 2d array
    String[][] my2dArray = new String[5][1];

    // nested for loop to create, populate and print my2dArray
    for (int row = 0; row < my2dArray.length; row++){ // number of rows in my2dArray
        for (int column = 0; column < my2dArray[row].length; column++){ // number of columns in my2dArray
            System.out.print(my2dArray[row][column]+"\t");
        }
        System.out.println();
    }
}

1 个答案:

答案 0 :(得分:0)

声明这个数组:

String[] entries = new String[]{ "File",..."Exit"  };

并像这样使用它:

do {
    try {
        // menu to be displayed
        System.out.println("Menu");
        for( int i = 0; i < entries.length; ++i ){
            System.out.println((i+1) + "." + entries[i] );
        }
        System.out.println("Select option.");
        // request input from the user
        menuOption = menuScanner.nextInt();
        // if else statements to determine the user's answer
        if( 1 <= menuOption && menuOption <= entries.length ){
            System.out.println("You have selected " + entries.[menuOptions-1] );
        } else {
            System.out.println("Invalid selection" );
        }
    } catch (InputMismatchException inputMismatchException){
        // catch block
        menuScanner.nextLine();
        System.out.println("Numbers only please.");
    }
} while (menuOption != 5);

不需要二维数组。