简体   繁体   English

如何将成员添加到团队

[英]how to Add members to the Team

I want the program to go back to the Menu when the switch cases has been completed or is over but i don't know how to do it. 我希望程序在开关盒已完成或结束时返回菜单,但我不知道该怎么做。 here's my code. 这是我的代码。 example, when the user has chosen his/her choice and he/she has performed it the program closes automatically, but i don't want that i want it to continue until the user wishes to exit the program. 例如,当用户选择了自己的选择并执行了该操作后,程序将自动关闭,但是我不希望我继续执行该操作,直到用户希望退出该程序为止。 please help i don't know what to do 请帮助我不知道该怎么办

    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;

     }


  }
}
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');

use a loop, in this case I'm using a do while loop to reloop the menu. 使用循环,在这种情况下,我使用do while循环重新循环菜单。 Bring down your println options so it prints the menu for the user to see again. 调低您的println选项,以便打印菜单供用户再次查看。 Your choice should be inside the loop too. 您的选择也应该在循环内。

Also you are creating a lot of same Scanner object, you just need one and you can re-use it since all of them are using System.in, you are just wasting your memory space by creating same objects for the same use. 另外,您正在创建许多相同的Scanner对象,您只需要一个对象就可以重复使用,因为它们全部都在使用System.in,您通过创建用于相同用途的相同对象来浪费内存空间。 Look in my case with your codes, I'm using just using the "ans" variable which already stores the Scanner(System.in) and re-use them for any of the user "System Input" (System.in) instead of re-declaring a new one. 就您的代码而言,我只使用已经存储了Scanner(System.in)的“ ans”变量,然后将其用于任何用户“ System Input”(System.in),而不是重新宣布一个新的。

try like this! 尝试这样!

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);
 }

}}} }}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM