我的计算不能正常工作。我看不出代码有什么问题。有时它不能正确计算分数。有时它做得很完美。我什至无法理解它什么时候做得好,什么时候做得不好。
分数计算应该是这样的:
Ace 可以在总分上加 1 或 11。如果得分高于 21,则 ace 计算为 1;否则 ace 是 11。
这是我的代码:
// Updates the the value of the cards the player has in their hand
int updateValueOfHand() {
int result = 0; // it will be returned
int ace = 0; // value of ace
for (int i =0; i < playerHand.size(); i++) // loop to see players hand
{
int cardValue; // card value of hand
Card card=(Card)playerHand.get(i); // check the card
cardValue = card.getRank();
if (cardValue == 1) // if card value is 1 (ace)
{
cardValue = 0; // assign to 0
ace += 1; // ace is 1 (if there are 2 aces ace is 2 ...)
}
result = result + cardValue; // result is card value (no ace)
}
//return result;
println("Number of ace: " + ace);
if (ace!=0) //if there is ace
{
for (int j=0; j<ace; j++) // if there is more than 1 ace
{
if (result+11<=21) { // if result is <= 21 when you count ace as 11, then ace is 11
result+=11;
}
else {
result+=1; // otherwise ace is 1
}
}
}
return result;
}