我现在拥有的东西:我目前有一个公共课“MainMenu”,在那里我向用户致意并要求他们输入一组数字(1-10)以获取他们喜欢的选项执行。这是由Switch案例决定的。
在每个案例中,要么运行一个小的执行,要么指向一个单独的类。
例如:
public static void mainMenu(Scanner sc) {
//Scanner sc = new Scanner(System.in);
System.out.println("Welcome to the Flight Scheduler!\n");
System.out.println("Please remember to always use U, M, T, W, R, F, S, for entering");
System.out.println("the day of the week, and to always use military time");
System.out.println("for entering the time.");
System.out.println("Please make your choice\nby entering the corresponding menu number:\n");
System.out.println("1.\tSet Clock");
System.out.println("2.\tClear Schedule");
System.out.println("3.\tAdd Airline");
System.out.println("4.\tAdd Flight");
System.out.println("5.\tCancel Flight");
System.out.println("6.\tShow Flight Info");
System.out.println("7.\tShow Departures");
System.out.println("8.\tShow Arrivals");
System.out.println("9.\tFind Flights Between Two Airports");
System.out.println("10.\tExit\n");
int userChoice = sc.nextInt();
switch (userChoice) {
case 1:
AirlineAircraftData.AddAirline(sc);
break;
这将执行我的 AddAirline 方法,我想将对象返回主菜单,并允许用户在输入后立即继续从之前提供的10个选项中进行选择 AddAirline 方法请求的字段。
以下是我在用户选择后运行的方法:
public static A8AirlineAircraftData AddAirline(Scanner sc) {
sc.nextLine();
System.out.println("Please enter the Airline name:");
String airName = sc.nextLine();
System.out.println("Please enter the Airline code:");
String airCode = sc.nextLine();
System.out.println("Please enter the Delta Aircraft:");
String airCraft = sc.nextLine();
System.out.println("Please enter the first class seat capacity:");
int firstClass = sc.nextInt();
System.out.println("Please enter the business class seat capacity:");
int busiClass = sc.nextInt();
System.out.println("Please enter the economy class seat capacity:");
int econClass = sc.nextInt();
System.out.println("Airline name: " + airName);
System.out.println("Airline code: " + airCode);
System.out.println("Delta Aircraft: " + airCraft);
//Splitting the first word from the rest of the string
String arr[] = airCraft.split(" ", 2);
String firstWord = arr[0];
System.out.println(firstWord + " first class seat capacity: " + firstClass);
System.out.println(firstWord + " business class seat capacity: " + busiClass);
System.out.println(firstWord + " economy class seat capacity: " + econClass);
//Airline object
A8AirlineAircraftData airline = new A8AirlineAircraftData(airName, airCode, airCraft, firstClass, busiClass, econClass);
System.out.println(airName + " successfully added. Press Enter to continue.");
sc.nextLine();//Press Enter to continue
sc.nextLine();
//A8MainMenu.mainMenu(sc); //return to main menu after Enter.
return airline;
感谢您的帮助,我是一名新手,我知道我正在做的很多事情并不一定有意义。
答案 0 :(得分:0)
由于@D M,我能够弄清楚如何解决这个问题。我只是将整个主菜单包裹在退出' 10'的do-while循环中。