如何将我在选项 1 和 2 中的输入打印到选项 3?这是我的代码

我想在选项 3 中打印选项 1 和 2 的输入。我有一个菜单设置,用户选择他们想要的数字,然后在他们选择哪个团队之后有后续问题。


    if (choys == 1)

    {

        ch = new Scanner(System.in);

        System.out.println("Enter name: ");

        userBName = ch.nextLine();


        System.out.println("Enter age: ");

        userBAge = ch.nextInt();


        if(userBAge >=18 && userBAge <=21)

        {

            System.out.println("Congrats "+userBName+"! Welcome to the team.");

            bslot++;

        }

        else

        {

            System.out.println("Sorry "+userBName+". You are not qualified.");

        }


    }


    if (choys == 2)

    {

        System.out.println("Enter name: ");

        userVName = ch.nextLine();


        System.out.println("Enter age: ");

        userVAge = ch.nextInt();


        if(userVAge >=18 && userVAge <=21)

        {

            System.out.println("Congrats "+userVName+"! Welcome to the team.");

            vslot++;

        }

        else

        {

            System.out.println("Sorry "+userVName+". You are not qualified.");

        }

    }


    if (choys == 3)

    {

        System.out.println("Current number of recruits:\n");

        System.out.println("Basketball team: "+ userBName+"\n\n");

        System.out.println("Volleyball team: "+ userVName);

    }

    }


慕莱坞森
浏览 91回答 2
2回答

慕尼黑5688855

我们可以使用 2 个不同的 ArrayList 来存储选项 1 和 2 中的用户名。然后在选项 3 中打印相同的 ArrayList。List<String> vNames = new ArrayList();List<String> bNames = new ArrayList();if (choys == 1){&nbsp; &nbsp; ch = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter name: ");&nbsp; &nbsp; userBName = ch.nextLine();&nbsp; &nbsp; System.out.println("Enter age: ");&nbsp; &nbsp; userBAge = ch.nextInt();&nbsp; &nbsp; if(userBAge >=18 && userBAge <=21)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Congrats "+userBName+"! Welcome to the team.");&nbsp; &nbsp; &nbsp; &nbsp; bslot++;&nbsp; &nbsp; &nbsp; &nbsp; bNames.add(userBName);&nbsp; &nbsp; }&nbsp;}if (choys == 2){&nbsp; &nbsp; ch = new Scanner(System.in);&nbsp; &nbsp; System.out.println("Enter name: ");&nbsp; &nbsp; userVName = ch.nextLine();&nbsp; &nbsp; System.out.println("Enter age: ");&nbsp; &nbsp; userVAge = ch.nextInt();&nbsp; &nbsp; if(userVAge >=18 && userVAge <=21)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Congrats "+userVName+"! Welcome to the team.");&nbsp; &nbsp; &nbsp; &nbsp; vSlot++;&nbsp; &nbsp; &nbsp; &nbsp; vNames.add(userVName);&nbsp; &nbsp; }&nbsp;}&nbsp;if (choys == 3){&nbsp; &nbsp; System.out.println("Current number of recruits:\n");&nbsp; &nbsp; System.out.println("Basketball team: ");&nbsp; &nbsp; for(String name:bNames){&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Username : "+ name);&nbsp; &nbsp; }}

猛跑小猪

我曾尝试通过使用 while 循环让 vNames 和 bNames 留在 ArrayList 中来扩展@Shashank 的代码。&nbsp; &nbsp; &nbsp; &nbsp; Scanner ch = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; List<String> vNames = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; List<String> bNames = new ArrayList<String>();&nbsp; &nbsp; &nbsp; &nbsp; int choys;&nbsp; &nbsp; &nbsp; &nbsp; int bslot = 0, vslot = 0;&nbsp; &nbsp; &nbsp; &nbsp; Boolean run = true;&nbsp; &nbsp; &nbsp; &nbsp; while (run) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter Choice");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; choys = Integer.parseInt(ch.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (choys == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter name: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String userBName = ch.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter age: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int userBAge = Integer.parseInt(ch.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userBAge >= 18 && userBAge <= 21) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Congrats " + userBName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "! Welcome to the team.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bslot++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bNames.add(userBName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else if (choys == 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ch = new Scanner(System.in);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter name: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String userVName = ch.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter age: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int userVAge = Integer.parseInt(ch.next());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (userVAge >= 18 && userVAge <= 21) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Congrats " + userVName&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; + "! Welcome to the team.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vslot++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; vNames.add(userVName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (choys == 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Current number of recruits:\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Basketball team: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String name : bNames) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Username : " + name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Volleyball team: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (String name : vNames) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Username : " + name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Program Ended");
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java