当前位置: 首页 > 知识库问答 >
问题:

在用户键入从Java中的另一个方法返回到主菜单的选项后,是否有一种方法来显示main方法?

潘哲
2023-03-14

我正在做一个Java项目,但我有点卡住了,主要方法是要求用户从菜单中输入一个数字来显示它,按下10它将用户带到分级子菜单方法,在那里用户可以选择从菜单中键入一个数字1-4来显示它,因为他们也可以键入0来退出,我的想法是如果键入0,它会返回到显示主要方法,有没有办法做到这一点?以下是我到目前为止的代码:

import java.util.Scanner;
public class MidTermProject {

    public static void main(String[] args) {
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Here is the sample of menu choices for Main Menu.");
        
        System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student" +
                            "\n2. Create Course" + "\n3. Create Enrollment" + "\n4. Edit Student"
                            + "\n6. Edit Enrollment" + "\n8. Display Course" + "\n9. Display Enrollment"
                            + "\n10. Grades Sub Menu" + "\n0. --- Quit ---");
        
        System.out.println("Please enter a valid choice(1-10, 0 to Quit) :");
        int userInput = keyboard.nextInt();
        
        if(userInput == 1) {
            CreateStudent();
        } else if(userInput == 10) {
            GradesSubMenu();
        }

    }
    
    public static void CreateStudent() {
        String FullName;
        String address;
        String city;
        String state;
        int zipcode;
        String phone;
        String email;
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
        FullName = keyboard.next();
        
        System.out.print("Address: ");
        address = keyboard.next();
        address = keyboard.next();
        
        System.out.print("City: ");
        city = keyboard.next();
        
        System.out.print("State: ");
        state = keyboard.next();
        state = keyboard.next();
        
        System.out.print("Zip Code: ");
        zipcode = keyboard.nextInt();
        
        System.out.print("Phone Number(format xxx-xxx-xxxx): ");
        phone = keyboard.next();
        
        System.out.print("Email Address: ");
        email = keyboard.next();
        
    }
    
    public static void GradesSubMenu() {
        int userInput;
        
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("\nGrades Sub Menu:\n" + "\nGrades Menu" + "\n1. View Grades By Student"
                            + "\n2. View Grades by Course" + "\n3. Edit Grades by Student" + 
                            "\n4. Edit Grades by Course" + "\n0. -- Exit to Menu --");
        
        System.out.print("Please enter a valid choice(1-4, 0 to Exit):");
        userInput = keyboard.nextInt();
        
        *if(userInput == 0) {
            main(String[] args);//how to reference to display back the main method
        }*
    }

}

共有1个答案

夏侯昊明
2023-03-14

如果确实需要,可以使用main(null);代替main(string[]args);

我对你的类做了一些修改...我建议你不要只使用静态方法。

import java.util.Scanner;

    public class MidTermProject {

    private Scanner scanner;

    public MidTermProject(Scanner scanner) {
        super();
        this.scanner = scanner;
    }

    public static void main(String[] args) {
        Scanner keyboardInputScanner = new Scanner(System.in);

        MidTermProject midTermProject = new MidTermProject(keyboardInputScanner);
        midTermProject.showMainMenu();
    }

    private void showMainMenu() {
        System.out.println("Here is the sample of menu choices for Main Menu.");

        System.out.println("\nWelcome to University Enrollment" + "\n1. Create Student" + "\n2. Create Course"
                + "\n3. Create Enrollment" + "\n4. Edit Student" + "\n6. Edit Enrollment" + "\n8. Display Course"
                + "\n9. Display Enrollment" + "\n10. Grades Sub Menu" + "\n0. --- Quit ---");

        System.out.println("Please enter a valid choice(1-10, 0 to Quit) :");
        int userInput = scanner.nextInt();

        if (userInput == 1) {
            createStudent();
        } else if (userInput == 10) {
            gradesSubMenu();
        }
    }

    public void createStudent() {
        String fullName;
        String address;
        String city;
        String state;
        int zipcode;
        String phone;
        String email;

        System.out.print("\nPlease enter your information bellow.\n" + "\nFull Name: ");
        fullName = scanner.next();

        System.out.print("Address: ");
        address = scanner.next();
        address = scanner.next();

        System.out.print("City: ");
        city = scanner.next();

        System.out.print("State: ");
        state = scanner.next();
        state = scanner.next();

        System.out.print("Zip Code: ");
        while (!scanner.hasNextInt()) { // make sure that the user gets an error if he enters a text, this could work
            scanner.next();
            System.out.println("Plase enter only numbers");
        }
        zipcode = scanner.nextInt();

        System.out.print("Phone Number(format xxx-xxx-xxxx): ");
        phone = scanner.next();

        System.out.print("Email Address: ");
        email = scanner.next();

        System.out.println(fullName);

    }

    public void gradesSubMenu() {
        System.out.println(
                "\nGrades Sub Menu:\n" + "\nGrades Menu" + "\n1. View Grades By Student" + "\n2. View Grades by Course"
                        + "\n3. Edit Grades by Student" + "\n4. Edit Grades by Course" + "\n0. -- Exit to Menu --");

        System.out.print("Please enter a valid choice(1-4, 0 to Exit):");
        int userInput = scanner.nextInt();

        if (userInput == 0) {
            System.out.println("0");
            this.showMainMenu();
        }
    }

    }
 类似资料:
  • 问题内容: 我有 但后来当我打电话从通过运行以下命令在命令行上,JAVA抱怨说,你不能调用从静态功能的方法。 所以我的问题是:如何从主方法调用方法,如果不可能,使用java命令从命令行运行程序后,有哪些替代策略可以调用方法。 问题答案: 您只能针对类的实例调用类似的实例方法(顺便说一下,这是一个非法的方法名称): 另外,如果对您的设计有效,则也使它成为静态。

  • 我试图创建一个简单的程序来输出由用户输入的星星的数量。我正在尝试学习如何使用一个以上的方法来做到这一点,这是我的代码 我面临的问题是,在循环方法中,我不能使用变量n,有没有办法在main方法中使用变量,在另一个方法中使用变量?泰 -平古

  • 我最近开始使用MapStruct,并有这个疑问。因此,可以将映射从一个方法继承到另一个方法,该方法用于映射前一个方法输出的子类。 对于例如-如果我有一个Car类,它需要映射成两个类FourWheeler和DetailedFourWheeler(扩展了FourWheeler类),我可以使用为将Car转换成FourWheeler而定义的映射到将Car转换成DetailedFourWheeler的方法中

  • 问题内容: 因此,页面充满了很多类似的属性,例如下面的两个元素。例子: 我正在使用PageObject和PageFactory来获取元素,并将它们放入这样的数组中: 问题是这样的:我需要单击这些类似的按钮之一,而不使用索引( 因为事实证明,要维护它很麻烦 )。我想到了一种方法,我可以收集列表中的所有webElement并返回显示在屏幕上的单个webElement(我乐于接受新想法),所以这是我尝试

  • 在Spock规范中,expect:或then:block中的任何行都被计算并断言为,除非它具有返回类型为的签名。 方法在类中定义如下: 我故意在那里断言,这样它就不会失败。即使失败并出现错误: 如何以及为什么方法调用结果被计算为?

  • 有没有Angular2的方法可以做到这一点,或者我只是使用老式的JavaScript?