【发布时间】:2020-10-29 20:29:27
【问题描述】:
我已经编写了以下应用程序,一切正常。然而,在用户输入对应于菜单命令的值(1-7)后,该命令被执行,然后程序终止。我正在尝试找到一种方法,允许我在显示命令结果后要求用户“按 Enter”,以重新打印菜单并继续输入命令,直到它们准备好退出 (8)。这意味着按下回车将重新打印菜单并允许输入更多命令(1-8)。我知道这需要作为一个循环来完成,但我不确定在这种情况下使用哪个/如何使用循环。谢谢。
import java.util.*;
import java.io.*;
public class RosterApplication {
Scanner in = null;
public static void main(String[] args) {
Roster studentRoster = new Roster();
studentRoster.loadData("assg3_roster.txt");
printMenu();
Scanner keyboard = new Scanner(System.in);
String entry = keyboard.next();
if (entry.equals("1")) { //Display the roster
studentRoster.displayRoster();
}
//System.out.println("Press enter to continue:");
//String reload = keyboard.nextLine();
//if (reload.equals("")) {
// printMenu();
//}
if (entry.equals("2")) { //Search for a student by id
System.out.println("Please enter the student id to search: ");
String searchId = keyboard.next();
if ((studentRoster.searchForStudent(searchId)) == null) {
System.out.println("Student not found!");
}
}
if (entry.equals("3")) { //Add a new student
System.out.println("Please enter the student id to add: ");
String addId = keyboard.next();
if (studentRoster.searchForStudent(addId) != null) {
System.out.println("Error: This student already exists! Student's info displayed above." + "\n");
} else {
System.out.println("Please enter the student name: ");
String addName = keyboard.next() + " " + keyboard.next();
System.out.println("Please enter the student standing: ");
String addStanding = keyboard.next();
System.out.println("Please enter the student major: ");
String addMajor = keyboard.next();
studentRoster.addStudent(addId, addName, addStanding, addMajor);
System.out.println("The entered information has succesfully been added as a new student." + "\n");
}
}
if (entry.equals("4")) { //Remove a student
System.out.println("Please enter the student id to remove: ");
String removeId = keyboard.next();
studentRoster.removeStudent(removeId);
}
if (entry.equals("5")) { //Search for student by major
System.out.println("Please enter the major to search: ");
String majorSearch = keyboard.next();
ArrayList < Student > students = studentRoster.getStudentByMajor(majorSearch);
if (students.size() != 0) {
System.out.println("The students with this major are listed above.");
} else {
System.out.println("Error: There are no students with this major!");
}
}
if (entry.equals("6")) { //Sort and save to file
studentRoster.Sort();
studentRoster.Save();
}
if (entry.equals("7")) { //Save to file
studentRoster.Save();
}
if (entry.equals("8")) { //Exit
studentRoster.Save();
System.exit(0);
}
keyboard.close();
}
private static void printMenu() {
System.out.println("1. Display the roster");
System.out.println("2. Search for a student by id");
System.out.println("3. Add a new student");
System.out.println("4. Remove a student");
System.out.println("5. Search for students by major");
System.out.println("6. Sort and save to file");
System.out.println("7. Save to file");
System.out.println("8. Exit");
}
}
【问题讨论】:
-
因为无论如何你都以
System.exit()退出(虽然这是一个坏习惯),你可以在整个while(true)方法周围放置一个大循环,除了前两行,初始化扫描仪及其关闭。但是,如果您将System.exit()替换为break;会更干净,这样扫描仪就会正确关闭。另请查看How to Ask,然后相应地编辑您的标题。How to get multiple inputs from user会更合适。 -
成功了!非常感谢:)下次这是我发布的第一个问题时,我会确保使用更好的标题。
-
edit 你的问题永远不会太迟。
-
你的问题已经解决了吗?
标签: java loops input menu equals