无法使我在嵌套循环中创建的菜单正常运行

时间:2017-11-29 04:01:32

标签: java menu nested nested-loops submenu

我正在尝试创建需要使用大量菜单的程序(作为一个初学者,这些东西真的开始压倒我,但它是我班级的项目)。这就是我到目前为止所做的:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;


public class GroupUs {

public static void main(String[] args) {


    String fileName = "CompConClass.txt"; //File includes class roster  

    System.out.println("Hello, would you like to access the class that you have on file or would you like to create a new class?");

    int choice = mainMenu();
    int choice2 = subMenu();

    while (choice != 0) {
        if (choice == 1) {  

            subMenu(); //calls subMenu method

        if (choice2 == 1 ) {

            try {
                BufferedReader br = new BufferedReader(new FileReader(fileName));

                String line = null; //create a line variable 
                while ((line = br.readLine()) != null) { //this will read the file line by line

                    System.out.println(line); //displays every line
                }

              } catch (FileNotFoundException ex) {
                  ex.printStackTrace();
              } catch (IOException ex) {
                  ex.printStackTrace();
              }


        }


        } else if (choice == 2) {
            System.out.println("test");
        }

        choice = mainMenu();

        }

    }        

    public static int mainMenu() {
        Scanner scan = new Scanner(System.in);  // Reading from System.in
        System.out.println( "Press 0 to quit.\n"
                + "Press 1 to access the class that you have on file.\n"
                + "Press 2 to create a new class.\n"
                );
        return scan.nextInt(); // Scans the next token of the input as an int.
    }

    public static int subMenu() {
        Scanner scan = new Scanner(System.in);  // This method will give the teacher the ability to view the roster from the file or add students on to that file
        System.out.println( "What would you like to do to the class on file?\n"
                + "Press 1 to view the students.\n"
                + "Press 2 to add another student.\n"
                + "Press 3 to remove a student."
                );
        return scan.nextInt(); // Scans the next token of the input as an int.
    }      

}

具体来说,我在代码的这一部分遇到了麻烦

  if (choice == 1) {  

        subMenu(); //calls subMenu method

    if (choice2 == 1 ) {

        try {
            BufferedReader br = new BufferedReader(new FileReader(fileName));

            String line = null; //create a line variable 
            while ((line = br.readLine()) != null) { //this will read the file line by line

                System.out.println(line); //displays every line
            }

          } catch (FileNotFoundException ex) {
              ex.printStackTrace();
          } catch (IOException ex) {
              ex.printStackTrace();
          }

正在发生的事情是程序最初通过向用户呈现我创建的第一个mainMenu方法而开始。当我输入数字1以打开我的subMenu方法时,它也能正常工作。但是,当我再次按1(这次显示文件中的名单)在subMenu的一侧时,它只会再次打印出subMenu。我再次按1,然后根据需要向我显示名单。我无法弄清楚为什么我第一次无法显示它。

1 个答案:

答案 0 :(得分:1)

int choice2 = subMenu();

while (choice != 0) {
    if (choice == 1) {  

        subMenu(); //calls subMenu method

您正在调用subMenu()方法两次,这就是它被运行两次的原因。