Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';我在嵌套循环中创建的菜单无法正常运行_Java_Menu_Nested_Nested Loops_Submenu - Fatal编程技术网

Java Can';我在嵌套循环中创建的菜单无法正常运行

Java Can';我在嵌套循环中创建的菜单无法正常运行,java,menu,nested,nested-loops,submenu,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 { pub

我正在尝试创建一个需要使用大量菜单的程序(作为一个初学者,这个东西真的开始让我不知所措,但它是为我的课程设计的)。这就是我到目前为止所做的:

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();
          }
所发生的事情是,通过向用户展示我创建的第一个主菜单方法,程序一开始运行良好。当我输入数字1以打开我的子菜单方法时,它也能正常工作。但是,当我在子菜单的一侧再次按1(这次显示文件上的花名册)时,它只是再次打印出子菜单。我再次按1,然后它会根据需要显示花名册。我不明白为什么我不能在第一次展示它

int choice2 = subMenu();

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

        subMenu(); //calls subMenu method

您正在调用
子菜单()
方法两次,这就是为什么它要运行两次。

Hey@D M感谢您的建议!这对我来说澄清了太多,我感到有点惭愧,因为我现在更好地理解了它,所以我不得不问这个问题,但我想这是过程的一部分。我还有一个问题,如果我想从子菜单返回主菜单,你会建议我怎么做?在我的程序中,从一个菜单跳到另一个菜单是必不可少的。如果你有我能读到的东西,或者像这样的东西的视频,我也会接受!感谢你的方式,它并不坏;主菜单位于围绕子菜单的循环中。