【发布时间】:2026-01-20 08:20:03
【问题描述】:
我有一个函数来计算任何给定卡片的总价值,它将所有当前卡片的总和存储到分数中。 它还计算一副牌中的 A 数。 我无法在循环后将 ace 的值和 score 联系起来。 我已经在我的实际文件中实现了打印语句,它计算正确,但是在 for 循环之后 score 和 ace 显然填充了随机数。 我不知道如何在 for 循环之外使用这些值。
void calculateScore(Player * current_player)
{
int i, ace;
unsigned score;
ace = 0;
score = 0;
for (i=0; i<current_player->curnumcards; i++){
if (current_player->hand[i].c_face == 0){
ace++;
} else if (current_player->hand[i].c_face == 10){
score += 10;
} else if (current_player->hand[i].c_face == 11){
score += 10;
} else if (current_player->hand[i].c_face == 12){
score += 10;
} else {
score += ++current_player->hand[i].c_face;
}//end if to check ace/jack/queen/king
}//end for loop
if (ace>0){
for (;ace!=1;ace--){
if (score>11){
score += 1;
} else {
score += 11;
}//end if > 10 ace
}//end for loop to calculate ace's
}// end if ace loop
printf("Current score: %d\n", &score);
}
【问题讨论】:
-
这很可疑:
score += ++current_player->hand[i].c_face;。我想你的意思是score += current_player->hand[i].c_face + 1;,除非你打算换手牌!
标签: c variables for-loop counter local-variables