【问题标题】:Can't get a menu to function properly that I created within a nested loop无法让我在嵌套循环中创建的菜单正常运行
【发布时间】:2018-05-12 16:38:02
【问题描述】:

我正在尝试创建需要使用大量菜单的程序(作为初学者,这些东西真的开始让我不知所措,但它是为我的班级设计的)。这是我目前所拥有的:

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 方法时,它也可以正常工作。但是,当我在 subMenu 一侧再次按 1(这次是显示文件中的名单)时,它只是再次打印出 subMenu。我再次按 1,然后它会根据需要向我显示名册。我不知道为什么我不能让它在第一次显示。

【问题讨论】:

    标签: java menu nested nested-loops submenu


    【解决方案1】:
    int choice2 = subMenu();
    
    while (choice != 0) {
        if (choice == 1) {  
    
            subMenu(); //calls subMenu method
    

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

    【讨论】:

    • 嘿@DM 感谢您的建议!这对我来说很清楚,我感到有点羞耻,因为我现在已经更好地理解了它,但我想这是过程的一部分。我还有一个问题,如果我想从子菜单返回主菜单,你会如何建议我这样做?在我的程序中,从一个菜单跳到另一个菜单将是必不可少的。如果您有我可以阅读的内容或类似此类的视频,我也会接受!谢谢
    • 你拥有它的方式还不错;主菜单在子菜单周围的循环中。
    猜你喜欢
    • 1970-01-01
    • 2014-05-28
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多