计算两张二十一点牌的总值

标签 c

我需要编写一个函数,将两张牌的二十一点手牌的值作为输入,并打印出这手牌的总点数。这是我目前所拥有的,但总返回 0。

int card_score(int num_of_cards)
{


char card_value[SIZE];
int ace_seen=0, total=0;

for (int i=0; i<num_of_cards;i++)

{
    switch (card_value[i])
    {

        case 'a': case 'A':
            total +=11;
            ace_seen++;
            break;
        case 'k': case 'K':
        case 'q': case 'Q':
        case 'j': case 'J':
        case 't': case 'T':
            total +=10;
            break;

        case '9':
            total +=9;
            break;

        case '8':
            total +=8;
            break;

        case '7':
            total +=7;
            break;

        case '6':
            total +=6;
            break;

        case '5':
            total +=5;
            break;

        case '4':
            total +=4;
            break;

        case '3':
            total +=3;
            break;

        case '2':
            total +=2;
            break;

        default:  
            printf("Invalid cards. Please try again.");                         
            break;


    }
}
         return total;

}


int main()
{
    char card_value[SIZE];
    int num_of_cards = 0;
    int total;
    int i = 0;

    printf("\n\nEnter cards: ");
    scanf("%c", &card_value[i]);

    total  = card_score(num_of_cards);
    printf("\n\nYour score is %d: \n",total);
    return 0;
}

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

你的 num_of_cards 是 0。它甚至没有进入循环。也许要求用户输入或将其硬编码为您真正想要的内容

关于计算两张二十一点牌的总值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19779959/

相关文章:

c - SQLite3 和限制结果的数量

c - 如何对 char var[][] 进行 shmat,C

c - 将 String 转换为 unsigned long 的问题

c++ - GSL 编译错误

c++ - 赋值后 Long double 重置为 0

c - 如何从 C 文件更改终端中的目录

c - 在 C 中使用 rand() 生成多个随机数时出现段错误

CS50x pset3(为什么我的程序没有通过Check50?)

c - 如何访问双指针类型参数作为二维数组?

c - 动态内存分配问题