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

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

如何減少 If 語句?

如何減少 If 語句?

偶然的你 2023-08-18 10:34:01
我是一個(gè)超級(jí)初學(xué)者程序員,我正在制作一個(gè)簡單的應(yīng)用程序,它根據(jù)高中項(xiàng)目的輸入選擇猴子(gog)。我查過這個(gè)問題,所有的答案似乎都是針對(duì)具體情況的,或者超出了我的理解范圍,所以 ELI5 的答案將不勝感激。這是我的代碼供參考:    //variables    var bananas;    var color;    var size;        //updates color on click    onEvent("colorDD", "click", function( ) {      updateScreen();    });        //updates size on click    onEvent("sizeDD", "click", function( ) {      updateScreen();    });        //updates bananas consumed on slider move    onEvent("bananaSlider", "mousemove", function( ) {      updateScreen();    });        //Updates the screen with all info including bananas, color, and size to display Gog image        function updateScreen() {            //color storer      color = getText("colorDD");            //size storer      size = getText("sizeDD");              //banana storer      bananas = getNumber("bananaSlider");                  //If statement for bottom text      if (color == "Red" && bananas == 10 && size == "Big"){ // Big Red Gogs        setText("feedbackOutput", "Gog -1?!?!?!");        setImageURL("gog", "gog-1.gif");            } else if ( color == "Red" && size == "Big" && bananas < 5){        setText("feedbackOutput", "You are Gog 720!");        setImageURL("gog", "gog720.gif");            } else if ( color == "Red" && size == "Big"){        setText("feedbackOutput", "You are Gog 6!");        setImageURL("gog", "gog6.gif");              } else if ( color == "Red" && size == "Medium" && bananas > 6){ // Medium Red Gogs        setText("feedbackOutput", "You are Gog 51!");        setImageURL("gog", "gog51pog-min.gif");                } else if ( color == "Red" && size == "Medium" && bananas < 4){         setText("feedbackOutput", "You are Gog 5!");        setImageURL("gog", "gog5.gif");                } 可以看出,有香蕉、顏色和大小三個(gè)變量,為每個(gè)變量選擇特定值將在屏幕上給出不同的圖像。
查看完整描述

3 回答

?
回首憶惘然

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

您可以將信息存儲(chǔ)在對(duì)象中,然后迭代它們并檢查:


const gogs = [

    {color: "Red", size: "Big", cmp: "==", bananas: 10, fb: "Gog -1?!?!?!", img: "gog-1.gif"},

    {color: "Red", size: "Big", cmp: "<", bananas: 5, fb: "You are gog 720!", img: "gog720.gif"},

    {color: "Red", size: "Big", cmp: "none", fb: "You are Gog 6!", img: "gog6.gif"},

    {color: "Red", size: "Medium", cmp: ">", bananas: 5, fb: "You are Gog 51!", img: "gog51pog-min.gif"},

    // continue on like this

    // we need some special ones that can't be easily expressed

    {special: true, cond: (color, size, bananas) => color == "Black" || bananas < 5, fb: "You are Gog 23!", img: "gog23.gif"},

    {special: true, cond: (color, size, bananas) => bananas > 5 && size != "Big", fb: "You are Gog 12!", img: "gog12.gif"},

    {special: true, cond: () => true, fb: "You are not a Gog yet!", img: "gogprequil.gif"}

];


const cmps = {

    "=="(a, b) { return a == b; },

    ">"(a, b) { return a > b; },

    "<"(a, b) { return a < b; },

    ">="(a, b) { return a >= b; },

    "<="(a, b) { return a <= b; }

};


// later in your code when checking gogs

for (const gog of gogs) {

    if (gog.special) {

        if (!gog.cond(color, size, bananas)) {

            continue;

        }

    } else {

        if (gog.color != color || gog.size != size) {

            continue;

        }

        if (gog.cmp != "none" && !cmps[gog.cmp](bananas, gog.bananas)) {

            continue;

        }

    }

    setText("feedbackOutput", gog.fb);

    setImageURL("gog", gog.img);

    break;

}



查看完整回答
反對(duì) 回復(fù) 2023-08-18
?
慕森王

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

您想嘗試將數(shù)據(jù)與邏輯分開。這簡化了數(shù)據(jù)更新以及邏輯處理。首先,定義您的數(shù)據(jù)集:


const gogs = [

  { color: 'black', size: 'small', minBananas: 0, imgUrl: 'gog12-2.gif' },

  { color: 'black', size: 'small', minBananas: 6, imgUrl: 'gog34.gif' },

  // etc.

];

現(xiàn)在,當(dāng)您想要處理數(shù)據(jù)時(shí),可以執(zhí)行以下操作:


const gog = gogs.find((gog) => {

  gog.color === color &&

  gog.size === size &&

  gog.miniBananas <= bananaCount

});


if (!gog) {

  return;

}


setImageURL(gog.imgUrl);

另請(qǐng)參閱: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find


查看完整回答
反對(duì) 回復(fù) 2023-08-18
?
皈依舞

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

您有很多重復(fù)的color和size參數(shù)。您可以按照 Aplet123 的建議進(jìn)行操作,但這可能無法幫助您思考對(duì) if 語句進(jìn)行分組的一般策略。


更簡單的方法可能是這樣的


    if (color == "Red") { // group by "Red" first--anything that is "Red" will go into this block

        if (size == "Big") { // next group by "Big" -- anything "Red" + "Big" will go into this block

            if (bananas == 10) { // so if we get true here, we know we are Red + Big + bananas = 10

                setText("feedbackOutput", "Gog -1?!?!?!");

                setImageURL("gog", "gog-1.gif");

            }

            else if (bananas < 5) { // if we get true here, we know we are Red + Big + bananas < 5

                setText("feedbackOutput", "You are Gog 720!");

                setImageURL("gog", "gog720.gif");

            } 

            else {

                setText("feedbackOutput", "You are Gog 6!");

                setImageURL("gog", "gog6.gif");

            } 

        } // end of block of conditions for "Big" + "Red"

        else if (size == "Medium" ) { // group by "Medium" 

            if (bananas > 6) {

                setText("feedbackOutput", "You are Gog 51!");

                setImageURL("gog", "gog51pog-min.gif");  

        

            } 

            else if ( bananas < 4){ 

                setText("feedbackOutput", "You are Gog 5!");

                setImageURL("gog", "gog5.gif");  

        

            } 

            else {

                setText("feedbackOutput", "You are Gog 33!");

                setImageURL("gog", "gog33.gif");

            }

      } // end of Red + Medium

      // next we would try the "Small" ones (left as an exercise to reader)

  }

  else if (color == "Brown") {

    // similar to above, group by sizes then for each size check banana conditions

  }

 // etc 

這根本不會(huì)節(jié)省長度,但可以節(jié)省每個(gè)if語句的復(fù)雜性,這可能是初學(xué)者應(yīng)該考慮的問題。


查看完整回答
反對(duì) 回復(fù) 2023-08-18
  • 3 回答
  • 0 關(guān)注
  • 204 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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