当前位置: 首页 > 知识库问答 >
问题:

JavaScript:我的开关语句总是默认为第一种情况

牟飞沉
2023-03-14

我正在制作一个21点游戏,可以用javascript帮助计算牌数。其中一个功能是更新计数。为了做到这一点,我放了一个switch语句,如果卡片是2到6,那么计数将递增1,如果卡片是杰克、国王、王后或王牌,那么计数将递减1。我已经检查了我的代码多次,无法找出错误所在。无论变量dcac等于什么,它将始终默认为switch语句中的第一种情况。我甚至尝试设置ac=“three”,但它仍然默认为第一种情况。有人能找出问题所在吗?谢谢

只需更新计数功能:

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name; //String
    console.log("DC: " + dc);
    switch(dc){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
        dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = playerCards[p].name; //String
    console.log("PC: " + pc);
    switch(pc){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
      default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
  //setText("countLbl", "Count: " + (playerCount + dealerCount));
}
var dealerCards = [];

控制台输出:

DC=ace

da

DC=九

D2

PC=3

p2

PC=3

p2

我的整个程序(注意:它不会在堆栈溢出中运行,因为它是在code.org的应用程序实验室中编写的,我被迫为我的类使用)

/* TODO:
 * 1) Make play again button
 * 2) Make reset button
 * 3) Make count work
 * 4) Make hide/show count button
 * 5) Make first dealer card hidden
 * 6) Check for blackjack on first deal
 * 7) Make Ace = 1 if total is greater than 21
 * 8) Make deck re-shuffle upon reset or cards run out
*/







//var Count = 0;
var playerCards = [];
var dealerCards = new Array();
var deckArr = new Array();



//Stores the properties each card can have
var cardProp = {
  suit: ['heart', 'spade', 'club', 'diamond'],
  value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 11, 11],
  name: ['ace', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'jack', 'queen', 'king']
};

/*--------------------------------------------------------------------------------------------*/
console.log(playerCards.length);
populateDeck();
//console.log(deckArr.length);
//printDeck();

//deckArr = shuffleDeck(deckArr).slice(0);
printDeck();
console.log("----------------------------");

//STAND BUTTON
onEvent("standBtn", "click", function(){
  //Check player for blackjack
  if(getPlayerTotal() > 21){
    showElement("winLoseLbl");
    setText("winLoseLbl", "BUST! \n Dealer Wins!");
  } else {
    //Deal until dealer total is greater than 17
    //then check for win
    while(getDealerTotal() < 17){
      dealerHit();
    }
    
    //Check for dealer or player win
    if(getDealerTotal() < getPlayerTotal() || getDealerTotal() > 21){
      showElement("winLoseLbl");
      setText("winLoseLbl", "Player Wins!!");
    } else {
      showElement("winLoseLbl");
      setText("winLoseLbl", "Dealer Wins!!");
    }
  }
});



//START BUTTON
onEvent("startBtn", "click", function(){
  hideElement("startBtn");
  dealPlayerCards();
  dealDealerCards();
  updateCount();
  //TEMP Hide cards used for hit button
  hideElement("dealerCard3");
  hideElement("dealerCard4");
  hideElement("playerCard3");
  hideElement("playerCard4");
  
});

//DEAL BUTTON
//deal 2 cards to the player and 2 to the dealer
onEvent("dealBtn", "click", function(){
  //Deal player cards
  for(var c = 0; c < 2; c += 1){

    console.log("Deck Arr Length: " + deckArr.length);
    playerCards[c] = drawCard();
    console.log("Deck Arr Length: " + deckArr.length);
    displayCard(playerCards[c], "playerCard" + (c + 1));
    console.log(playerCards[c]);
    

  }
});


//HIT BUTTON

onEvent("hitBtn", "click", function(){
  //Draws a card then displays it in the first hidden card
  //slot if not already displayed, otherwise displays
  //in the second hidden card slot
  if(getProperty("playerCard3","hidden") === true){
    playerCards[2] = drawCard();
    showElement("playerCard3");
    displayCard(playerCards[2], "playerCard" + 3);
  } else if(getProperty("playerCard3", "hidden") === false && getProperty("playerCard4", "hidden") === true){
    playerCards[3] = drawCard();
    showElement("playerCard4");
    displayCard(playerCards[3], "playerCard" + 4);
  }
  updateCount();

  
});




/*--------------------------------------------------------------------------------------------*/

function dealerHit(){
  if(getProperty("dealerCard3","hidden") === true){
    dealerCards[2] = drawCard();
    showElement("dealerCard3");
    displayCard(dealerCards[2], "dealerCard" + 3);
  } else if(getProperty("dealerCard3", "hidden") === false && getProperty("dealerCard4", "hidden") === true){
    dealerCards[3] = drawCard();
    showElement("dealerCard4");
    displayCard(dealerCards[3], "dealerCard" + 4);
  }
}


function getDealerTotal(){
  var total = 0;
  for(var x = 0; x < dealerCards.length; x += 1){
    total += dealerCards[x].value;
  }
  return total;
}



function getPlayerTotal(){
  var total = 0;
  for(var x = 0; x < playerCards.length; x += 1){
    total += playerCards[x].value;
  }
  console.log(total);
  return total;
}



function dealPlayerCards(){
    for(var c = 0; c < 2; c += 1){

      //console.log("Deck Arr Length: " + deckArr.length);
      playerCards[c] = drawCard();
      //console.log("Deck Arr Length: " + deckArr.length);
      displayCard(playerCards[c], "playerCard" + (c + 1));
     // console.log(playerCards[c]);


  }
}


function dealDealerCards(){
    for(var c = 0; c < 2; c += 1){

      //console.log("Deck Arr Length: " + deckArr.length);
      dealerCards[c] = drawCard();
      //console.log("Deck Arr Length: " + deckArr.length);
      displayCard(dealerCards[c], "dealerCard" + (c + 1));
      //console.log(dealerCards[c]);
     

  }
}


//Checks the deltCards array to see if a card was delt




//Draws a random card from the deck
function drawCard(){
  var randomNum = randomNumber(0, deckArr.length - 1);
  var returnCard;
  //console.log("Random Num " + randomNum);
  
  returnCard = deckArr[randomNum];
  deckArr.splice(randomNum, 1);
  //printDeck();
  return returnCard;
}


//Displays the input card in the inputted position
function displayCard(card, position){
  if(card !== undefined){
    getKeyValue(card.name + "_" + card.suit, function(image){
      setProperty(position, "image", image);
    });
  } else {
    console.log("ERROR: NAME undefined");
    write("UNDEFINED");
  }
}



//Prints the deck for debugging
function printDeck(){
  for(var x = 0; x < deckArr.length; x += 1){
    console.log("#: " + x + " S: " + deckArr[x].suit + " V: " + deckArr[x].value + " N: " + deckArr[x].name);
  }
}


//Sets each index of deckArr to
//a new object with the PROPERTIES: Value, Name, Suit, Image
function populateDeck(){

  var count = 0;
  
  for(var s = 0; s < cardProp.suit.length; s += 1)
    for(var v = 0; v < cardProp.value.length; v += 1){
      //Create new object 
      //ready to store properties in
      deckArr[count] = {};
      
      //Set Properties to object created above
      deckArr[count].value = cardProp.value[v];
      deckArr[count].name = cardProp.name[v];
      deckArr[count].suit = cardProp.suit[s];

      count += 1;
    }
}




function shuffleDeck(deck){
  var newDeck = new Array(deck.length); 
  var randomNum = randomNumber(0, deck.length - 1);
  
  
  for(var c = deck.length - 1; c >= 0; c -= 1){
    while(newDeck[randomNum] !== undefined){ //need to fix
      randomNum = randomNumber((0, deck.length - 1));
    }
    newDeck[randomNum] = deck[c];
    randomNum = randomNumber(0, deck.length - 1);
  }
  return newDeck;
  
}


//Checks for a win in the player or the dealer
function checkWin(){
  for(var x = 0; x < playerCards.length; x += 1){
    
  }
}

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name;
    console.log("DC: " + dc);
    switch(dc.valueOf()){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
      dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = "three";
    console.log("PC: " + pc);
    switch(pc.valueOf){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
        default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
  setText("countLbl", "Count: " + (playerCount + dealerCount));
}

共有1个答案

江温书
2023-03-14
dealerCards=[{name:'ace'},{name:'two'}]
playerCards=[{name:'three'},{name:'three'}]
updateCount();

//Update the count
function updateCount(){
  var dc;
  var pc;
  var dealerCount = 0;
  var playerCount = 0;
  
  for(var d = 0; d < dealerCards.length; d += 1){
    
    dc = dealerCards[d].name; //String
    console.log("DC: " + dc);
    switch(dc.valueOf()){
      case "two":
        dealerCount += 1;
        console.log("d2");
        break;
      case "three":
        dealerCount += 1;
        console.log("d3");
        break;
      case "four":
        dealerCount += 1;
        console.log("d4");
        break;
      case "five":
        dealerCount += 1;
        console.log("d5");
        break;
      case "six":
        dealerCount += 1;
        console.log("d6");
        break;
      case "jack":
        dealerCount -= 1;
        console.log("dj");
        break;
      case "queen":
        dealerCount -= 1;
        console.log("dq");
        break;
      case "king":
        dealerCount -= 1;
        console.log("dk");
        break;
      case "ace":
        dealerCount -= 1;
        console.log("da");
        break;
      default:
        dealerCount += 0;
        
    }
  }
  
  for(var p = 0; p < playerCards.length; p += 1){
    pc = playerCards[p].name; //String
    console.log("PC: " + pc);
    switch(pc.valueOf()){
      case "two":
        playerCount += 1;
        console.log("p2");
        break;
      case "three":
        playerCount += 1;
        console.log("p3");
        break;
      case "four":
        playerCount += 1;
        console.log("p4");
        break;
      case "five":
        playerCount += 1;
        console.log("p5");
        break;
      case "six":
        playerCount += 1;
        console.log("p6");
        break;
      case "jack":
        playerCount -= 1;
        console.log("pj");
        break;
      case "queen":
        playerCount -= 1;
        console.log("pq");
        break;
      case "king":
        playerCount -= 1;
        console.log("pk");
        break;
      case "ace":
        playerCount -= 1;
        console.log("pa");
        break;
      default:
        playerCount += 0;
        
    }
  }
  //count = Count + Count
 // setText("countLbl", "Count: " + (playerCount + dealerCount));
}
 类似资料:
  • 我想这样做: 这样的事情可能吗?我想防止代码重复。我知道在这个特定的示例中,没有什么理由这样做,因为重复的代码很小。我能想到的唯一一件事是这样的(使用开关盒的穿透能力): 我宁愿不这样做,因为增加案例数量并在底部设置默认值会更有意义。 但我想知道,如果我这样做,它会不会搞乱引擎盖下的goto语句?特别是,由于数字或顺序错误,决定使用哪个goto语句不会花费更长的时间吗?switch语句中的顺序重要

  • 所以基本上我有一个学校的项目,它包含一个time循环中的开关语句。 假设我输入“1”,它在大小写“1”中运行代码。然后它应该在之后中断。但默认代码也总是运行。 这是案例“1”代码: 这是默认代码: 以下是输出: 我很有信心我的代码是正确的,但是如果我缺少什么,请告诉我如何修复它,谢谢。

  • 问题内容: 对于建议抛出异常的人: 抛出异常不会给我编译时错误,而是给我运行时错误。我知道我会抛出异常,我宁愿在编译时死掉,也不愿在运行时死掉。 首先,我正在使用Eclipse 3.4。 我有一个数据模型,它的mode属性是Enum。 我目前正在编写此模型的视图,并且我有代码 我收到一个错误“此方法必须返回类型为java.awt.Color的结果”,因为我没有默认大小写,并且在函数末尾没有返回xx

  • 所以基本上我有一个学校项目,它在while循环中包含一个switch语句。 假设我输入“1”,它运行案例“1”中的代码。然后它会在之后破裂。但默认代码也总是可以运行。 以下是案例“1”代码: 这是默认代码: 以下是输出: 我很有信心我的代码是正确的,但是如果我缺少什么,请告诉我如何修复它,谢谢。

  • 我尝试过用0错误更改为if-else但仍然不起作用。我试图使用(string)$paramapi强制它字符串化,并将case值更改为case'1',但仍然不起作用。我还尝试用(integer)$paramapi和(int)$paramapi将它传递给integer,而情况1仍然不能正常工作。 请注意,我的swich变量在每一个... 我得到“无效的API调用1”,并希望得到“登录成功”或“登录失败

  • 我如何在 Angular 中编写适用于 3 个选项的开关大小写或 if 语句, 我已经试过了,但需要添加所有3个选项。