如果发牌人的总数不超过17张,我想从一副牌中添加卡片,直到我得到17张或更多。
错误发生的位置。
当两张牌的总价值不是17张或更多时,我想要添加牌,直到它变成17或更多。然而,使用这个代码,我尝试添加卡片,直到我得到17张或更多,但我添加了与第二张卡片相同的卡片(通过按stand按钮打开的卡片)。已经关门了。17岁以上怎么加卡?
发布于 2021-08-15 14:59:50
这个问题是由您的for循环引起的,在函数turnDealer中调用dealer_3Img后,出现了一个"break",注释如下:
function turnDealer(dealerTotal) {
if(dealerTotal < 17){
for (let i = dealerTotal; dealerTotal < 17; i++) {
console.log(i);
//Get a new card from the array
var card = deck.pop();
dealer_3Img(card);
break //this will exit the loop
}
}
}请阅读“break”的用法:break语句“跳出”循环。https://www.w3schools.com/js/js
为了解决这个问题,你可以像@Aaron建议的那样使用while,你可以使用while循环来解决你的问题:https://www.w3schools.com/js/js
尝试将代码更改为:
function turnDealer(dealerTotal) {
while (dealerTotal < 17) {
console.log(i);
//Get a new card from the array
var card = deck.pop();
//need to update dealerTotal here, e.g. dealerTotal += card
dealer_3Img(card);
}
}发布于 2021-08-16 03:23:56
请原谅,我不怎么打牌..我不确定是否每张卡都有不同的dealerTotal,所以我的示例将值17视为17张卡
// If the dealer does not exceed 17, add cards until it exceeds 17.
function turnDealer (dealerTotal) {
var card = deck.pop()
var cardValue=1 //if cardValue is ever not 1, set it to whatever(since idk)
if (dealerTotal <17) {return turnDealer(dealerTotal+cardValue)}
return true //it has finished selecting
//usage: turnDealer(1) would make it take 17 cards, turnDealer(0) would make it take 18, turnDealer(2) would make it take 16 and so on
}https://stackoverflow.com/questions/68792553
复制相似问题