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

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

JavaScript 循環沒有在應該結束的時候結束

JavaScript 循環沒有在應該結束的時候結束

精慕HU 2023-08-10 15:41:08
我正在制作 21 點游戲(也稱為二十一點),我不確定為什么我的程序忽略這個條件語句,如果用戶的牌值高于 21,則循環結束,有人可以告訴我問題是什么這里會是?謝謝。(此代碼尚未完成,所以請原諒混亂。)(另請參閱“下面的問題”的評論)let cardRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];var cardOne = cardRange[Math.floor(Math.random()*cardRange.length)];var cardTwo = cardRange[Math.floor(Math.random()*cardRange.length)];var cardTotal = cardOne + cardTwo;var comScore = 18;var i;var extracard;alert(`Your card numbers are ${cardOne} and ${cardTwo}!`);//user gets to draw 5 cards in totalfor(i = 0; i<3;){var input = prompt(`Which makes your card total ${cardTotal}. Would you like to draw another card? (Type in 1 for yes, 0 for no, or select cancel to return to home.)`);if (input === null){    i+=3;    window.location.replace("http://stackoverflow.com");}else if (input === "1"){    i++;    extraCard = cardRange[Math.floor(Math.random()*cardRange.length)];    alert(`This card's number is ${extraCard}!`);    cardTotal = extraCard + cardTotal;}else if (input === "0"){    i+=3;}//problem belowelse if (cardTotal >=22){    i+=3;}//not working, loop will not end.else{alert("Lmao wrong input you rart");}}function pontoonOutput(){    if (cardTotal > comScore && cardTotal < 22){        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You win!`);    }    else if (cardTotal === comScore){        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... It is a tie!`);    }    //this outputs if the user gets above 22    else if (cardTotal >= 22){        alert("BUST!");        document.write(`Your card number is ${cardTotal}, which is above 21. Which means.... You lose!`);    }    //what should happen if line 29 is picked up. ^    else{        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You lose!`);    }    }        pontoonOutput();
查看完整描述

3 回答

?
慕田峪9158850

TA貢獻1794條經驗 獲得超7個贊

問題是您在相同的 if...else 語句中檢查卡總數,但在檢查“0”或“1”輸入之后。因此,如果用戶輸入“0”或“1”,您的代碼將處理這些條件,并且永遠不會進行卡總檢查。


有很多方法可以解決這個問題。一種解決方案是將卡總檢查移至主 if...else 邏輯之后的單獨 if 條件中。像這樣的東西:


for(i = 0; i<3;){

  var input = prompt(`Which makes your card total ${cardTotal}. Would you like to draw another card? (Type in 1 for yes, 0 for no, or select cancel to return to home.)`);


  if (input === null){

    // ...

  }

  else if (input === "1"){

    // ...

  }

  else if (input === "0"){

    // ...

  }


  // Keep separate from the main if...else logic to ensure it always gets run.

  if (cardTotal >=22){

    i+=3;

  }

}


查看完整回答
反對 回復 2023-08-10
?
紫衣仙女

TA貢獻1839條經驗 獲得超15個贊

問題是它是 的一部分else,所以只要他們輸入1或0或null,它就永遠不會命中。您可能只想最后將其移至其自身狀態:


let cardRange = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var cardOne = cardRange[Math.floor(Math.random() * cardRange.length)];

var cardTwo = cardRange[Math.floor(Math.random() * cardRange.length)];

var cardTotal = cardOne + cardTwo;

var comScore = 18;

var i;

var extracard;



alert(`Your card numbers are ${cardOne} and ${cardTwo}!`);

//user gets to draw 5 cards in total

for (i = 0; i < 3;) {

    var input = prompt(`Which makes your card total ${cardTotal}. Would you like to draw another card? (Type in 1 for yes, 0 for no, or select cancel to return to home.)`);



    if (input === null) {

        i += 3;

        window.location.replace("http://stackoverflow.com");

    } else if (input === "1") {

        i++;

        extraCard = cardRange[Math.floor(Math.random() * cardRange.length)];

        alert(`This card's number is ${extraCard}!`);

        cardTotal = extraCard + cardTotal;


    } else if (input === "0") {

        i += 3;

    } else {

        alert("Lmao wrong input you rart");

    }

    //problem below

    if (cardTotal >= 22) {

        i += 3;

    }

    //not working, loop will not end.


}



function pontoonOutput() {

    if (cardTotal > comScore && cardTotal < 22) {

        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You win!`);

    } else if (cardTotal === comScore) {

        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... It is a tie!`);

    }

    //this outputs if the user gets above 22

    else if (cardTotal >= 22) {

        alert("BUST!");

        document.write(`Your card number is ${cardTotal}, which is above 21. Which means.... You lose!`);

    }

    //what should happen if line 29 is picked up. ^

    else {

        document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You lose!`);

    }

}


pontoonOutput();


查看完整回答
反對 回復 2023-08-10
?
湖上湖

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

嘗試使用該break語句。試試這個:


for(i = 0; i<3;){

  var input = prompt(`Which makes your card total ${cardTotal}. Would you like to draw another card? (Type in 1 for yes, 0 for no, or select cancel to return to home.)`);


  if (input === null){

    // ...

  }

  else if (input === "1"){

    // ...

  }

  else if (input === "0"){

    // ...

  }

  

  if (cardTotal >=22){

    //i += 3;

    break;

  }

}

現在我明白了,它需要分開



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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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