第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

嘗試使用帶有“菜單”的 For-Loop

嘗試使用帶有“菜單”的 For-Loop

楊__羊羊 2022-11-30 13:42:25
初學(xué)者,請(qǐng)盡可能解釋!一個(gè)課程問(wèn)題要求我創(chuàng)建一個(gè)菜單(完成)。菜單上的多個(gè)選項(xiàng)給出不同的一次性結(jié)果(完成)?,F(xiàn)在它要我實(shí)現(xiàn)一個(gè)for,while和do...while循環(huán)(無(wú)法理解)我真的嘗試了我所有的基本知識(shí),包括在循環(huán)內(nèi)創(chuàng)建和填充數(shù)組for(事后看來(lái)這是一個(gè)愚蠢的想法)。public void displayMenu(){    System.out.println("A. Option #A");    System.out.println("B. Option #B");    System.out.println("C. Option #C");    System.out.println("D. Option #D");    System.out.println("X. Exit!");    System.out.println();    System.out.println("Please enter your choice:");}public void start(){    displayMenu();    Scanner console = new Scanner(System.in);    String input = console.nextLine().toUpperCase();    System.out.println();    switch (input)    {        case "A": System.out.println("Option #A was selected"); break;        case "B": System.out.println("Option #B was selected"); break;        case "C": System.out.println("Option #C was selected"); break;        case "D": System.out.println("Option #D was selected"); break;        case "X": System.out.println("You chose to Exit"); break;        default: System.out.println("Invalid selection made"); break;    }}public void startFor(){  /*Each of these methods will modify the original start() method, each    *will add a loop of the specific type so that the menu is displayed    *repeatedly, until the last option is selected. When the last option    *is selected, exit the method (i.e. stop the loop).   */}
查看完整描述

2 回答

?
手掌心

TA貢獻(xiàn)1942條經(jīng)驗(yàn) 獲得超3個(gè)贊

for正如您在評(píng)論中要求的示例一樣。


練習(xí)的重點(diǎn)似乎是在菜單上迭代,直到滿足退出條件 ( "X".equals(input))。這意味著在for語(yǔ)句中的三個(gè)條件之間,這是您唯一需要指定的條件。這是因?yàn)椋ɑ荆ゝor陳述的一般形式是


for ( [ForInit] ; [Expression] ; [ForUpdate] )

括號(hào)之間的這些術(shù)語(yǔ)都不是強(qiáng)制性的,因此我們也可以去掉[ForInit]and [ForUpdate](但保留分號(hào))。這具有不初始化任何東西的效果,[ForInit]并且在循環(huán)的每次迭代結(jié)束時(shí)什么也不做[ForUpdate],讓我們只檢查表達(dá)式給出的退出條件[Expression](當(dāng)它被評(píng)估為時(shí)false,循環(huán)退出)。


請(qǐng)注意,console是在循環(huán)外聲明的,因?yàn)樵诿看蔚鷷r(shí)都分配一個(gè)會(huì)很浪費(fèi)。而且input,因?yàn)槟趂or聲明的條件下需要它。


Scanner console = new Scanner(System.in);

String input = "";


for (;!"X".equals(input);) { // notice, the first and last part of the for loop are absent


    displayMenu();


    input = console.nextLine().toUpperCase();

    System.out.println();


    switch (input) {

        case "A": System.out.println("Option #A was selected"); break;

        case "B": System.out.println("Option #B was selected"); break;

        case "C": System.out.println("Option #C was selected"); break;

        case "D": System.out.println("Option #D was selected"); break;

        case "X": System.out.println("You chose to Exit"); break;

        default: System.out.println("Invalid selection made"); break;

    }

}

您可能會(huì)注意到這有點(diǎn)尷尬,因?yàn)檫@不是您通常使用for循環(huán)的目的。


無(wú)論如何,在這一點(diǎn)上,while版本變得微不足道 ( while (!"X".equals(input))) 并且在這種情況下,do...while也是等效的, ( do { ... } while (!"X".equals(input))) 因?yàn)橄嗤臈l件適用于當(dāng)前循環(huán)的末尾和下一個(gè)循環(huán)的開(kāi)始,并且有它們之間沒(méi)有副作用。


順便說(shuō)一句,您可能會(huì)注意到while (condition)和for (; condition ;)在功能上是等效的,并且您可能會(huì)想知道為什么應(yīng)該使用一個(gè)而不是另一個(gè)。答案是可讀性。做的時(shí)候想做什么就清楚多了while (condition)。


查看完整回答
反對(duì) 回復(fù) 2022-11-30
?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

for 循環(huán)中的所有參數(shù)都不是強(qiáng)制性的。定義一個(gè)停止標(biāo)志并檢查輸入是否為“X”。每當(dāng)輸入是“X”時(shí),只需更改 stopFlag 或只是簡(jiǎn)單地使用 break 語(yǔ)句來(lái)中斷循環(huán);



public void startFor()

    {

      boolean stopFlag = false;


      for(; stopFlag == false ;) {


        displayMenu();

        Scanner console = new Scanner(System.in);

        String input = console.nextLine().toUpperCase();

        System.out.println();


        switch (input)

        {

            case "A": System.out.println("Option #A was selected"); break;

            case "B": System.out.println("Option #B was selected"); break;

            case "C": System.out.println("Option #C was selected"); break;

            case "D": System.out.println("Option #D was selected"); break;

            case "X": System.out.println("You chose to Exit"); break;

            default: System.out.println("Invalid selection made"); break;

        }

        if(input.contentEquals("X"))

            stopFlag = true;

      }

    }


查看完整回答
反對(duì) 回復(fù) 2022-11-30
  • 2 回答
  • 0 關(guān)注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)