亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將我在選項 1 和 2 中的輸入打印到選項 3?這是我的代碼

如何將我在選項 1 和 2 中的輸入打印到選項 3?這是我的代碼

慕萊塢森 2022-12-28 16:41:02
我想在選項 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);    }    }
查看完整描述

2 回答

?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我們可以使用 2 個不同的 ArrayList 來存儲選項 1 和 2 中的用戶名。然后在選項 3 中打印相同的 ArrayList。


List<String> vNames = new ArrayList();

List<String> bNames = new ArrayList();


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++;

        bNames.add(userBName);

    }

 }


if (choys == 2)

{

    ch = new Scanner(System.in);

    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++;

        vNames.add(userVName);

    }

 }

 if (choys == 3)

{

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

    System.out.println("Basketball team: ");

    for(String name:bNames){

        System.out.println("Username : "+ name);

    }


}


查看完整回答
反對 回復 2022-12-28
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

我曾嘗試通過使用 while 循環讓 vNames 和 bNames 留在 ArrayList 中來擴展@Shashank 的代碼。


        Scanner ch = new Scanner(System.in);

        List<String> vNames = new ArrayList<String>();

        List<String> bNames = new ArrayList<String>();


        int choys;

        int bslot = 0, vslot = 0;

        Boolean run = true;

        while (run) {

            System.out.println("Enter Choice");

            choys = Integer.parseInt(ch.next());

            if (choys == 1) {

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

                String userBName = ch.next();


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

                int userBAge = Integer.parseInt(ch.next());


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

                    System.out.println("Congrats " + userBName

                            + "! Welcome to the team.");

                    bslot++;

                    bNames.add(userBName);

                }

            }else if (choys == 2) {

                ch = new Scanner(System.in);

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

                String userVName = ch.next();


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

                int userVAge = Integer.parseInt(ch.next());


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

                    System.out.println("Congrats " + userVName

                            + "! Welcome to the team.");

                    vslot++;

                    vNames.add(userVName);

                }

            } else if (choys == 3) {

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

                System.out.println("Basketball team: ");

                for (String name : bNames) {

                    System.out.println("Username : " + name);

                }

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

                for (String name : vNames) {

                    System.out.println("Username : " + name);

                }


            } else {

                run = false;

            }

        }


        System.out.println("Program Ended");


查看完整回答
反對 回復 2022-12-28
  • 2 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號