如何向团队添加成员

时间:2014-03-11 07:44:42

标签: java jcreator

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

    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;

     }


  }
}

2 个答案:

答案 0 :(得分: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 Input”(System.in)而不是重新宣布一个新的。

答案 1 :(得分: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);
 }

}}}

相关问题