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

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

如何減少 If 語句?

如何減少 If 語句?

偶然的你 2023-08-18 10:34:01
我是一個超級初學者程序員,我正在制作一個簡單的應用程序,它根據高中項目的輸入選擇猴子(gog)。我查過這個問題,所有的答案似乎都是針對具體情況的,或者超出了我的理解范圍,所以 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");                } 可以看出,有香蕉、顏色和大小三個變量,為每個變量選擇特定值將在屏幕上給出不同的圖像。
查看完整描述

3 回答

?
回首憶惘然

TA貢獻1847條經驗 獲得超11個贊

您可以將信息存儲在對象中,然后迭代它們并檢查:


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;

}



查看完整回答
反對 回復 2023-08-18
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

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


const gogs = [

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

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

  // etc.

];

現在,當您想要處理數據時,可以執行以下操作:


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

  gog.color === color &&

  gog.size === size &&

  gog.miniBananas <= bananaCount

});


if (!gog) {

  return;

}


setImageURL(gog.imgUrl);

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


查看完整回答
反對 回復 2023-08-18
?
皈依舞

TA貢獻1851條經驗 獲得超3個贊

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


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


    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 

這根本不會節省長度,但可以節省每個if語句的復雜性,這可能是初學者應該考慮的問題。


查看完整回答
反對 回復 2023-08-18
  • 3 回答
  • 0 關注
  • 194 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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