首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript循环在应该结束的时候没有结束

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

Stack Overflow用户
提问于 2020-11-18 04:25:08
回答 3查看 40关注 0票数 0

我正在做一个21人的游戏(也称为21点),我不确定为什么我的程序忽略了这个条件语句,如果用户的卡片值超过21,循环结束,有人能告诉我这里会有什么问题吗?谢谢。(这段代码还没有完成,所以请原谅我的混乱。)(另请参阅“下面的问题”的评论)

代码语言:javascript
复制
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();
EN

回答 3

Stack Overflow用户

发布于 2020-11-18 04:32:44

问题是它是else的一部分,所以只要他们输入10null,它就永远不会命中。你可能只想在最后把它移到它自己的状态:

代码语言:javascript
复制
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();

票数 1
EN

Stack Overflow用户

发布于 2020-11-18 04:32:48

问题是,您在相同的if...else语句中检查卡片总数,但在检查"0“或"1”输入之后。因此,如果用户输入"0“或"1",您的代码将处理这些条件,并且永远不会进行卡片总数检查。

有很多方法可以解决这个问题。一种解决方案是将卡总检查移到主if...else逻辑之后的单独if条件中。如下所示:

代码语言:javascript
复制
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;
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-11-18 04:37:19

尝试使用break语句。试着这样做:

代码语言:javascript
复制
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;
  }
}

现在我明白了,它需要拆分

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64882799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档