简体   繁体   English

为什么循环 While 不能正常工作?

[英]Why is the loop While not working properly?

I have a loop that creates cards until the dealer's score is exactly 17. But there is a problem, it creates either exactly 17 or more.我有一个创建卡片的循环,直到庄家的分数恰好是 17。但是有一个问题,它创建的恰好是 17 或更多。 I tried to solve it by reloading the page, but I don't think it's the best solution.我试图通过重新加载页面来解决它,但我认为这不是最好的解决方案。 There is a dealer variable that stores points.有一个存储点数的经销商变量。 There are cards card A has a weight of 11, cards with the letters J, K, Q have a weight of 10, and cards on which a number is depicted have the same weight as the number itself.卡片 A 的权重为 11,带有字母 J、K、Q 的卡片的权重为 10,而上面描绘数字的卡片的权重与数字本身相同。 In the cycle, you need to collect cards until the dealer's points (dealer variable) becomes 17. And now I don't know how to stop the cycle when the points already exceed 17. Cards are selected randomly循环中需要收牌,直到庄家点数(dealer变量)变成17点,现在点数已经超过17点不知道怎么停止循环,随机抽牌

 let dealer = 0 let player = 0 let value = [2,3,4,5,6,7,8,9,10,'A','J','K','Q'] let type = ['ПИКИ','ТРЕФИ','ЧЕРВИ','БУБЕН'] let coloda = [] for (let index = 0; index < value.length; index++) { for (let i2 = 0; i2 < type.length; i2++) { coloda.push(value[index] + ' ' + type[i2]) } } coloda.sort(()=> Math.random() - 0.5) console.log(coloda) function start(){ while(dealer < 17){ let random = Math.floor(Math.random()* coloda.length) let addcard = document.createElement('div') addcard.classList.add('card') addcard.textContent = coloda[random] document.querySelector('.dealer').appendChild(addcard) sum(random) } if(dealer> 17){ //reload document.querySelector('.dl').textContent = dealer }else{ console.log('good') document.querySelector('.dl').textContent = dealer } } function sum(random){ let data = coloda[random].split('')[0] if(data == 'A'){ dealer = dealer + 11 } else if(data == 'J' || data == 'K' || data =='Q'){ dealer = dealer + 10 }else{ dealer = dealer + Number(data) } } start()
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> <style>:card{ width; 100px: height; 100px: border; 1px solid black. } </style> </head> <body> <div class="dealer"> <h1 class="dl"></h1> </div> <script src="script.js"></script> </body> </html>

I tried to solve it by reloading the page, but I don't think it's the best solution.我试图通过重新加载页面来解决它,但我认为这不是最好的解决方案。

One thing you could do is check the value of the dealer every X seconds and proceed if it is above 17 like this (X should be a small number to make it act like a while loop)您可以做的一件事是每隔 X 秒检查一次经销商的价值,如果它像这样高于 17,则继续(X 应该是一个小数字,以使其像一个 while 循环)

function check() {
   if (dealer > 17) {
      return true
   } else {
      return false
   }
}

setInterval(function() {
   let valid = check();
   if (valid) {
      // Insert code here
   } else {
      console.log('Dealer is not above 17')
   }
}, X * 1000)

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM