0

我希望程序在切换案例完成或结束后返回菜单,但我不知道该怎么做。这是我的代码。例如,当用户选择了他/她的选择并且他/她已经执行了它时,程序会自动关闭,但我不希望它继续直到用户希望退出程序。请帮助我不知道该怎么办

    public class Jproj
    {
        public static void main(String[] args)
        {
            boolean back = false;
            String teamone = null;
            String teamtwo = null;
            LinkedList x = new LinkedList();
            String name;
            int team;
            char choice = 'w';


            int num=0;
            System.out.println("Menu");
            System.out.println("a.) Add a member");
            System.out.println("b.) Define the friends of each member");
            System.out.println("c.) Create two teams");
            System.out.println("d.) Save Data");
            System.out.println("e.) Extract data from file");
            Sysmte.out.println("f.) Exit");
            Scanner ans = new Scanner(System.in);
            System.out.print("Choice:");

             choice = ans.next().charAt(0);

             switch(choice)
             {
        case 'a':

            Scanner a = new Scanner(System.in);
            System.out.println("ADDING MEMBERS");
            System.out.print("Name of the member:");
            name = a.nextLine();
            System.out.println(""+name);

                x.insert(name);
                x.displayone();
                System.out.print("Successfull!");   

            break;
        case 'b':
            System.out.println("DEFINING FRIENDS OF MEMBERS");
            break;
        case 'c':
            System.out.print("Creating TEAMS");
            Scanner scanner = new Scanner(System.in);
            if(teamone!=null&&teamtwo!=null)
            {
                System.out.print("Teams has already been created!\n do you want to replace team names?");
                System.out.print("1-Yes\n2-No");

                int sagot = scanner.nextInt();
                if(sagot != 1)
                {
                    return;
                }
                else
                    System.out.println("CREATING TEAMS");
                    Scanner c = new Scanner(System.in);
                    System.out.print("Enter new Name of Team 1:");
                    teamone = c.nextLine();
                    System.out.print("Enter new Name of Team 2:");
                    teamtwo = c.nextLine();
                    x.teamname(teamone,teamtwo);
            }
            else
                    System.out.println("CREATING TEAMS");
                    Scanner c = new Scanner(System.in);
                    System.out.print("Name of Team 1:");
                    teamone = c.nextLine();
                    System.out.print("Name of Team 2:");
                    teamtwo = c.nextLine();
                    x.teamname(teamone,teamtwo);
                    System.out.print("Teams has been successfully created!");
                    System.out.print("Current members:");
                    x.displaytwo();
            break;
        case 'd':
            System.out.println("SAVING DATA");
            break;
        case 'e':
            break;
        case 'f':
            return;
            enter code herebreak;

     }


  }
}
4

2 回答 2

0

试试这样!

public class Jproj
{
    public static void main(String[] args)
    {
        boolean back = false;
        String teamone = null;
        String teamtwo = null;
        LinkedList x = new LinkedList();
        String name;
        int team;
        char choice = 'w';

    int num=0;
    while(true){
    System.out.println("Menu");
    System.out.println("a.) Add a member");
    System.out.println("b.) Define the friends of each member");
    System.out.println("c.) Create two teams");
    System.out.println("d.) Save Data");
    System.out.println("e.) Extract data from file");
    Sysmte.out.println("f.) Exit");
    Scanner ans = new Scanner(System.in);
    System.out.print("Choice:");

     choice = ans.next().charAt(0);

     switch(choice)
     {
case 'a':

    Scanner a = new Scanner(System.in);
    System.out.println("ADDING MEMBERS");
    System.out.print("Name of the member:");
    name = a.nextLine();
    System.out.println(""+name);

        x.insert(name);
        x.displayone();
        System.out.print("Successfull!");   

    break;
case 'b':
    System.out.println("DEFINING FRIENDS OF MEMBERS");
    break;
case 'c':
    System.out.print("Creating TEAMS");
    Scanner scanner = new Scanner(System.in);
    if(teamone!=null&&teamtwo!=null)
    {
        System.out.print("Teams has already been created!\n do you want to replace team names?");
        System.out.print("1-Yes\n2-No");

        int sagot = scanner.nextInt();
        if(sagot != 1)
        {
            return;
        }
        else
            System.out.println("CREATING TEAMS");
            Scanner c = new Scanner(System.in);
            System.out.print("Enter new Name of Team 1:");
            teamone = c.nextLine();
            System.out.print("Enter new Name of Team 2:");
            teamtwo = c.nextLine();
            x.teamname(teamone,teamtwo);
    }
    else
            System.out.println("CREATING TEAMS");
            Scanner c = new Scanner(System.in);
            System.out.print("Name of Team 1:");
            teamone = c.nextLine();
            System.out.print("Name of Team 2:");
            teamtwo = c.nextLine();
            x.teamname(teamone,teamtwo);
            System.out.print("Teams has been successfully created!");
            System.out.print("Current members:");
            x.displaytwo();
    break;
case 'd':
    System.out.println("SAVING DATA");
    break;
case 'e':
    break;
case 'f':
    System.exit(0);
 }

}}}

于 2014-03-11T08:06:35.643 回答
0
do {
    System.out.println("Menu");
    System.out.println("a.) Add a member");
    System.out.println("b.) Define the friends of each member");
    System.out.println("c.) Create two teams");
    System.out.println("d.) Save Data");
    System.out.println("e.) Extract data from file");
    Sysmte.out.println("f.) Exit");
    choice = ans.next().charAt(0);

    switch(choice){
        case 'a':

        System.out.println("ADDING MEMBERS");
        System.out.print("Name of the member:");
        name = ans.nextLine();
        System.out.println(""+name);

            x.insert(name);
            x.displayone();
            System.out.print("Successfull!");   

        break;
    case 'b':
        System.out.println("DEFINING FRIENDS OF MEMBERS");
        break;
    case 'c':
        System.out.print("Creating TEAMS");
        if(teamone!=null&&teamtwo!=null)
        {
            System.out.print("Teams has already been created!\n do you want to replace team names?");
            System.out.print("1-Yes\n2-No");

            int sagot = ans.nextInt();
            if(sagot != 1)
            {
                return;
            }
            else
                System.out.println("CREATING TEAMS");
                System.out.print("Enter new Name of Team 1:");
                teamone = ans.nextLine();
                System.out.print("Enter new Name of Team 2:");
                teamtwo = ans.nextLine();
                x.teamname(teamone,teamtwo);
        }
        else
                System.out.println("CREATING TEAMS");
                System.out.print("Name of Team 1:");
                teamone = ans.nextLine();
                System.out.print("Name of Team 2:");
                teamtwo = ans.nextLine();
                x.teamname(teamone,teamtwo);
                System.out.print("Teams has been successfully created!");
                System.out.print("Current members:");
                x.displaytwo();
        break;
    case 'd':
        System.out.println("SAVING DATA");
        break;
    case 'e':
        break;
    default:
            System.out.println("Invalid Option");
        break;
    } while (choice != 'f');

使用循环,在这种情况下,我使用 do while 循环来重新循环菜单。关闭您的 println 选项,以便打印菜单供用户再次查看。您的选择也应该在循环内。

此外,您正在创建许多相同的 Scanner 对象,您只需要一个并且您可以重复使用它,因为它们都使用 System.in,您只是通过为相同用途创建相同的对象来浪费您的内存空间。在我的情况下查看您的代码,我仅使用已存储 Scanner(System.in) 的“ans”变量并将它们重新用于任何用户“系统输入”(System.in) 而不是重新声明一个新的。

于 2014-03-11T08:04:53.063 回答