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



我正在做一个Java项目,但我有点拘泥于此,我的想法是主方法要求用户从菜单中输入一个数字来显示它,通过按10,它将用户带到等级子菜单方法,在那里用户可以选择从菜单中键入一个数字1-4来显示它,它会返回显示主要方法,有办法做到这一点吗?这是我到目前为止的代码:

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
}*
}
}

如果您真的想要,可以使用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();
}
}
}

最新更新