我正在制作 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();
慕田峪9158850
紫衣仙女
湖上湖
相关分类