我有以下简单的主菜单,没有调用其中的任何方法,当我在每个选择退出选项时,我迷失了如何让子菜单退回到主菜单? 我是初学者,这个项目非常重要,好像我失败了,尽管我已经付了钱,但我还是无法完成考试。(
菜单代码(目前为止)如下:
* //菜单将包含6个列表:书籍,会员,员工,贷款,统计,退出。
//这些列表从1到4将打开一个子菜单,包括:插入,搜索,删除,编辑,全部列出,退出。
//清单5将包括:前5本书,前5名成员,本月员工,名单覆盖,退出。
import java.util.*;
import java.io.*;
public class Menu
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
int option;
do
{
System.out.println("Main Menu:");
System.out.println("1. Books");
System.out.println("2. Members");
System.out.println("3. Employees");
System.out.println("4. Loans");
System.out.println("5. Statistics");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option) {
case 1: System.out.println("Books Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
option = sc.nextInt();
break;
}
break;
case 2: System.out.println("Members Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
option = sc.nextInt();
break;
}
break;
case 3: System.out.println("Employees Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
option = sc.nextInt();
break;
}
break;
case 4: System.out.println("Loans Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
option = sc.nextInt();
break;
}
break;
case 5: System.out.println("Statistics Sub-Menu:");
System.out.println("1. Top 5 Books");
System.out.println("2. Top 5 Members");
System.out.println("3. Emplyee of the Month");
System.out.println("4. List Overdue");
System.out.println("5. Exit");
System.out.println("Enter your option [1,2,3,4,5]:");
option = sc.nextInt();
switch (option){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
option = sc.nextInt();
break;
}
break;
case 6: System.out.println("You selected to Exit");
System.exit(0);
break;
}
} while (option!=6);
}
}*
答案 0 :(得分:0)
避免使用单个变量来控制两个不同的开关。在这里,您使用选项变量来控制内部和外部开关。当选项为6时,控制器从两个开关断开。 使用另一个变量来控制内循环。我在下面的代码中使用了subOption。
在用户想要退出之前,子菜单功能也应该有效。所以最好在内部开关内添加do。
public class Menu {
public static void main(String args[]) throws IOException {
Scanner sc = new Scanner(System.in);
int option, subOption;
do {
System.out.println("Main Menu:");
System.out.println("1. Books");
System.out.println("2. Members");
System.out.println("3. Employees");
System.out.println("4. Loans");
System.out.println("5. Statistics");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
option = sc.nextInt();
switch (option) {
case 1: {
do {
System.out.println("Books Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
subOption = sc.nextInt();
switch (subOption) {
case 1: {
System.out.println("in Insert");
break;
}
case 2: {
System.out.println("in Search");
break;
}
case 3: {
System.out.println("in Delete");
break;
}
case 4: {
System.out.println("in Edit");
break;
}
case 5: {
System.out.println("in List All");
break;
}
case 6: {
System.out.println("in Exit");
break;
}
// option = sc.nextInt();
}
} while (subOption != 6);
break;
}
case 2:
{
do {
System.out.println("Members Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
subOption = sc.nextInt();
switch (subOption) {
case 1: {
System.out.println("in Insert");
break;
}
case 2: {
System.out.println("in Search");
break;
}
case 3: {
System.out.println("in Delete");
break;
}
case 4: {
System.out.println("in Edit");
break;
}
case 5: {
System.out.println("in List All");
break;
}
case 6: {
System.out.println("in Exit");
break;
}
// option = sc.nextInt();
}
} while (subOption != 6);
break;
}
case 3:
{
do {
System.out.println("Employees Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
subOption = sc.nextInt();
switch (subOption) {
case 1: {
System.out.println("in Insert");
break;
}
case 2: {
System.out.println("in Search");
break;
}
case 3: {
System.out.println("in Delete");
break;
}
case 4: {
System.out.println("in Edit");
break;
}
case 5: {
System.out.println("in List All");
break;
}
case 6: {
System.out.println("in Exit");
break;
}
// option = sc.nextInt();
}
} while (subOption != 6);
break;
}
case 4:
{
do {
System.out.println("Loans Sub-Menu:");
System.out.println("1. Insert");
System.out.println("2. Search");
System.out.println("3. Delete");
System.out.println("4. Edit");
System.out.println("5. List All");
System.out.println("6. Exit");
System.out.println("Enter your option [1,2,3,4,5,6]:");
subOption = sc.nextInt();
switch (subOption) {
case 1: {
System.out.println("in Insert");
break;
}
case 2: {
System.out.println("in Search");
break;
}
case 3: {
System.out.println("in Delete");
break;
}
case 4: {
System.out.println("in Edit");
break;
}
case 5: {
System.out.println("in List All");
break;
}
case 6: {
System.out.println("in Exit");
break;
}
// option = sc.nextInt();
}
} while (subOption != 6);
break;
}
case 5: {
do {
System.out.println("Statistics Sub-Menu:");
System.out.println("1. Top 5 Books");
System.out.println("2. Top 5 Members");
System.out.println("3. Emplyee of the Month");
System.out.println("4. List Overdue");
System.out.println("5. Exit");
System.out.println("Enter your option [1,2,3,4,5]:");
subOption = sc.nextInt();
switch (subOption) {
case 1: {
System.out.println("in Top 5 Books");
break;
}
case 2: {
System.out.println("in Top 5 Members");
break;
}
case 3: {
System.out.println("in Emplyee of the Month");
break;
}
case 4: {
System.out.println("in List Overdue");
break;
}
case 5: {
System.out.println("in Exit");
break;
}
// option = sc.nextInt();
}
} while (subOption != 6);
break;
}
case 6:
System.out.println("You selected to Exit");
System.exit(0);
break;
}
} while (option != 6);
}
}