ausst
2021-10-08 20:57
采纳率: 100%
浏览 28

c语言结构体数组赋值/引用出错,运行时输出空

写的是一个扑克牌随机发牌,运行的时候直接输出空了,不知道哪里出了问题

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct
{
    char face;
    char point;
}POKE;
void main(void)
{
    const char *Face[4]={ "红桃", "方块", "梅花", "黑桃" };
    const char* Point[13] = { "1","2","3","4","5","6","7","8","9","10","J","K","L" };
    int i, j,a,m, num[52];
    POKE Player[4][13];
    srand((unsigned int)time(NULL));
    j = 1 + (int)(52 * rand() / (RAND_MAX + 1));
    num[0] = j;
    for (i = 1; i < 52; i++)
    {
        while(1)
        {
            j = 1 + (int)(52 * rand() / (RAND_MAX + 1));
            a = 0;
            for (a = 0; a < i; a++)
            {
                if (num[a] == j)
                    break;
            }
            if (a==i)
                break;
            
        } 
        num[i] = j;
    }
    printf("p1\t\tp2\t\tp3\t\tp4\n");
    for (m= 0; m< 52; m++)
    {
    
        i = num[m] % 4;
        j = num[m] / 4;
        Player[i][j].face = i;
        Player[i][j].point = j;
    }
    for (j = 0; j < 13; j++)
    {
        for (i = 0; i < 4; i++)
        {
            printf("%s%s\t", Face[Player[i][j].face], Point[Player[i][j].point]);
        }
        printf("\n");
    }

}

img

  • 好问题 提建议
  • 收藏

1条回答 默认 最新

  • CSDN专家-link 2021-10-08 21:01
    已采纳

    程序崩掉了,所以没有输出
    j = 1 + (int)(52 * rand() / (RAND_MAX + 1))
    改为
    j = (int)(52 * rand() / (RAND_MAX + 1))
    不应该加1。j的范围应该是0-51,你写成了1-52
    当j=52时,j/4=13。放到Player中会下标越界的

    已采纳该答案
    评论
    解决 1 无用
    打赏 举报

相关推荐 更多相似问题