【问题标题】:how can i loop a blackjack answer input我如何循环二十一点答案输入
【发布时间】:2021-12-20 10:20:45
【问题描述】:

我被困在二十一点任务上。我不知道如何让用户说出他们想要的多次hitMe Y/N。我的经销商面朝下。


let card = 0;
let total = 0;
let hitMe = true;
let dealer = 0;

//1st card
hitMe = prompt("Current total: " + total, "Take a card? Y/N");
// (hitMe == "Y"){hitMe = true;}
//se {hitMe = false;}
if(hitMe){
card = dealer = Math.ceil(11*Math.random()) + Math.ceil(11*Math.random());
total += card;
}


//final total
dealer = Math.ceil(11*Math.random()) + Math.ceil(11*Math.random()); //dealer always takes 2 cards
alert("Your hand is worth " + total + ". Dealer got " + dealer + ".");

【问题讨论】:

  • 请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: html loops blackjack


【解决方案1】:

使用 if 语句和 while 循环,如下所示:

  let card = 0;
  let total = 0;
  let hitMe = true;
  let dealer = 0;

  hitMe = prompt("Current total: " + total, "Take a card? Y/N");

  if(hitMe == "Y"){
    while(hitMe == "Y"){
      card = dealer = Math.ceil(11*Math.random()) + Math.ceil(11*Math.random());
      total += card;
      hitMe = prompt("Current total: " + total, "Take a card? Y/N");
    }
  } else {
    dealer = Math.ceil(11*Math.random()) + Math.ceil(11*Math.random());
    alert("Your hand is worth " + total + ". Dealer got " + dealer + ".");
  }

如果用户没有输入 Y,将显示最终的总数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2018-04-23
    • 1970-01-01
    • 2013-12-08
    • 2015-06-18
    • 1970-01-01
    • 2017-01-15
    相关资源
    最近更新 更多