JavaScript 循环没有在应该结束的时候结束

我正在制作 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 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;

}

//problem below

else 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();


精慕HU
浏览 107回答 3
3回答

慕田峪9158850

问题是您在相同的 if...else 语句中检查卡总数,但在检查“0”或“1”输入之后。因此,如果用户输入“0”或“1”,您的代码将处理这些条件,并且永远不会进行卡总检查。有很多方法可以解决这个问题。一种解决方案是将卡总检查移至主 if...else 逻辑之后的单独 if 条件中。像这样的东西:for(i = 0; i<3;){&nbsp; 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.)`);&nbsp; if (input === null){&nbsp; &nbsp; // ...&nbsp; }&nbsp; else if (input === "1"){&nbsp; &nbsp; // ...&nbsp; }&nbsp; else if (input === "0"){&nbsp; &nbsp; // ...&nbsp; }&nbsp; // Keep separate from the main if...else logic to ensure it always gets run.&nbsp; if (cardTotal >=22){&nbsp; &nbsp; i+=3;&nbsp; }}

紫衣仙女

问题是它是 的一部分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 totalfor (i = 0; i < 3;) {&nbsp; &nbsp; 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.)`);&nbsp; &nbsp; if (input === null) {&nbsp; &nbsp; &nbsp; &nbsp; i += 3;&nbsp; &nbsp; &nbsp; &nbsp; window.location.replace("http://stackoverflow.com");&nbsp; &nbsp; } else if (input === "1") {&nbsp; &nbsp; &nbsp; &nbsp; i++;&nbsp; &nbsp; &nbsp; &nbsp; extraCard = cardRange[Math.floor(Math.random() * cardRange.length)];&nbsp; &nbsp; &nbsp; &nbsp; alert(`This card's number is ${extraCard}!`);&nbsp; &nbsp; &nbsp; &nbsp; cardTotal = extraCard + cardTotal;&nbsp; &nbsp; } else if (input === "0") {&nbsp; &nbsp; &nbsp; &nbsp; i += 3;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; alert("Lmao wrong input you rart");&nbsp; &nbsp; }&nbsp; &nbsp; //problem below&nbsp; &nbsp; if (cardTotal >= 22) {&nbsp; &nbsp; &nbsp; &nbsp; i += 3;&nbsp; &nbsp; }&nbsp; &nbsp; //not working, loop will not end.}function pontoonOutput() {&nbsp; &nbsp; if (cardTotal > comScore && cardTotal < 22) {&nbsp; &nbsp; &nbsp; &nbsp; document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You win!`);&nbsp; &nbsp; } else if (cardTotal === comScore) {&nbsp; &nbsp; &nbsp; &nbsp; document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... It is a tie!`);&nbsp; &nbsp; }&nbsp; &nbsp; //this outputs if the user gets above 22&nbsp; &nbsp; else if (cardTotal >= 22) {&nbsp; &nbsp; &nbsp; &nbsp; alert("BUST!");&nbsp; &nbsp; &nbsp; &nbsp; document.write(`Your card number is ${cardTotal}, which is above 21. Which means.... You lose!`);&nbsp; &nbsp; }&nbsp; &nbsp; //what should happen if line 29 is picked up. ^&nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; document.write(`You got ${cardTotal}, and the AI player got ${comScore}. Which means.... You lose!`);&nbsp; &nbsp; }}pontoonOutput();

湖上湖

尝试使用该break语句。试试这个:for(i = 0; i<3;){&nbsp; 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.)`);&nbsp; if (input === null){&nbsp; &nbsp; // ...&nbsp; }&nbsp; else if (input === "1"){&nbsp; &nbsp; // ...&nbsp; }&nbsp; else if (input === "0"){&nbsp; &nbsp; // ...&nbsp; }&nbsp;&nbsp;&nbsp; if (cardTotal >=22){&nbsp; &nbsp; //i += 3;&nbsp; &nbsp; break;&nbsp; }}现在我明白了,它需要分开
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript