为什么mod在';仅限于<=1. Card*createCard(){ /*此函数动态分配一个新的Card struct对象并返回 指向该结构对象的指针,该结构对象稍后将用于插入到 链表。有三种类型的牌:攻击牌、防御牌和跑动牌。 攻击和防御牌也有价值。 您将根据这些随机机会分配卡类型: 40%攻击:该值是一个介于1和5之间(含1和5)的随机数。 50%-防御:该值是一个介于3和8之间(含3和8)的随机数。 10%-运行:该值是一个介于1和8之间(含1和8)的随机数。 跑步卡的值仅用于排序目的*/ 卡片*createdCard; int n; INTV; createdCard=(卡片*)malloc(卡片大小); n=(rand()%10)+1; 如果(n==1){ 创建卡片->卡片类型=运行; v=(rand()%8)+1; createdCard->value=v; } 否则如果(n>1&&n卡片类型=防御; v=(rand()%6)+3; createdCard->value=v; } 否则如果(n>6&&ncardType=攻击; v=(rand()%5)+1; createdCard->value=v; } createdCard->next=NULL; 返回createdCard; }

为什么mod在';仅限于<=1. Card*createCard(){ /*此函数动态分配一个新的Card struct对象并返回 指向该结构对象的指针,该结构对象稍后将用于插入到 链表。有三种类型的牌:攻击牌、防御牌和跑动牌。 攻击和防御牌也有价值。 您将根据这些随机机会分配卡类型: 40%攻击:该值是一个介于1和5之间(含1和5)的随机数。 50%-防御:该值是一个介于3和8之间(含3和8)的随机数。 10%-运行:该值是一个介于1和8之间(含1和8)的随机数。 跑步卡的值仅用于排序目的*/ 卡片*createdCard; int n; INTV; createdCard=(卡片*)malloc(卡片大小); n=(rand()%10)+1; 如果(n==1){ 创建卡片->卡片类型=运行; v=(rand()%8)+1; createdCard->value=v; } 否则如果(n>1&&n卡片类型=防御; v=(rand()%6)+3; createdCard->value=v; } 否则如果(n>6&&ncardType=攻击; v=(rand()%5)+1; createdCard->value=v; } createdCard->next=NULL; 返回createdCard; },c,random,linked-list,malloc,function-pointers,C,Random,Linked List,Malloc,Function Pointers,根据声明 Card *createCard() { /* This function dynamically allocates a new Card struct object and returns a pointer to that struct object which will later be used to insert into a linked list. There are three types of cards ATTACK, DEFEND, and R

根据声明

Card *createCard() {

  /* This function dynamically allocates a new Card struct object and returns a 
  pointer to that struct object which will later be used to insert into a 
  linked list. There are three types of cards ATTACK, DEFEND, and RUN. 
  ATTACK and DEFEND cards also have a value. 
  You will assign a card type based on these random chances:
    40% - ATTACK: the value is a random number between 1 and 5 inclusive.
    50% - DEFEND: the value is a random number between 3 and 8 inclusive.
    10% - RUN: the value is a random number between 1 and 8 inclusive. 
    The value of a RUN card is only used for sorting purposes.*/
    
    Card *createdCard;
    int n;
    int v; 
    createdCard = (Card *)malloc(sizeof(Card));
    
    n = (rand() % 10) + 1;
    
    if (n == 1) {
        createdCard->cardType = RUN;
        v = (rand() % 8) + 1;
        createdCard->value = v;
    }
    else if (n > 1 && n < 7) {
        createdCard->cardType = DEFEND;
        v = (rand() % 6) + 3;
        createdCard->value = v;
    }
    else if (n > 6 && n < 10) {
        createdCard->cardType = ATTACK;
        v = ( rand() % 5) + 1;
        createdCard->value = v;
    }
    
    createdCard->next = NULL;
    
    return createdCard;
}
将为
n
分配一个从1到10(包括1和10)的整数

n
1
时,将实现
n==1

n
10
时,将执行
n>1&&n6&&ncardType
createdCard->value
,其值将保持不确定

如果您使用不确定的值,您将调用未定义的行为,似乎您碰巧得到了零

要解决此问题,您应该添加
else
语句来覆盖
n=10
案例,并通过该语句为
createdCard->cardType
createdCard->value
赋值

Card *createCard() {

  /* This function dynamically allocates a new Card struct object and returns a 
  pointer to that struct object which will later be used to insert into a 
  linked list. There are three types of cards ATTACK, DEFEND, and RUN. 
  ATTACK and DEFEND cards also have a value. 
  You will assign a card type based on these random chances:
    40% - ATTACK: the value is a random number between 1 and 5 inclusive.
    50% - DEFEND: the value is a random number between 3 and 8 inclusive.
    10% - RUN: the value is a random number between 1 and 8 inclusive. 
    The value of a RUN card is only used for sorting purposes.*/
    
    Card *createdCard;
    int n;
    int v; 
    createdCard = (Card *)malloc(sizeof(Card));
    
    n = (rand() % 10) + 1;
    
    if (n == 1) {
        createdCard->cardType = RUN;
        v = (rand() % 8) + 1;
        createdCard->value = v;
    }
    else if (n > 1 && n < 7) {
        createdCard->cardType = DEFEND;
        v = (rand() % 6) + 3;
        createdCard->value = v;
    }
    else if (n > 6 && n < 10) {
        createdCard->cardType = ATTACK;
        v = ( rand() % 5) + 1;
        createdCard->value = v;
    }
    
    createdCard->next = NULL;
    
    return createdCard;
}
将为
n
分配一个从1到10(包括1和10)的整数

n
1
时,将实现
n==1

n
10
时,将执行
n>1&&n6&&ncardType
createdCard->value
,其值将保持不确定

如果您使用不确定的值,您将调用未定义的行为,似乎您碰巧得到了零

要解决此问题,您应该添加
else
语句来覆盖
n=10
案例,并为
createdCard->cardType
createdCard->value
假设
rand()
返回
9
,如下:

n=(rand()%10)+1

然后
n
将是
10

但是没有处理这种情况的条件语句。这可能会导致
createdCard->value
变为
0
,因为它尚未初始化,而且可用内存稀疏

当兰德返回19,29,39,49。。。假设
rand()
返回
9
,则有10%的时间

n=(rand()%10)+1

然后
n
将是
10

但是没有处理这种情况的条件语句。这可能会导致
createdCard->value
变为
0
,因为它尚未初始化,而且可用内存稀疏


当兰德返回19,29,39,49。。。当
n==10
时,有10%的时间你会错过这个案例。改用
else
(如果
,则不使用
)。当第一个操作数可被第二个操作数整除时,mod将返回零。什么是
它被限制为
n
n这里有什么特别返回0的?0是我认为OP的意思是它的
=1
(因为
+1
)。他们认为
(rand()%10)+1
导致了零(但事实上,他们得到的是
10
)你错过了
n==10
的情况。改用
else
(如果
,则不使用
)。当第一个操作数可被第二个操作数整除时,mod将返回零。什么是
它被限制为
n
n这里有什么特别返回0的?0是我认为OP的意思是它的
=1
(因为
+1
)。他们认为
(rand()%10)+1
导致了零(但事实上,他们得到了
10
)是的!这纠正了问题!否则{createdCard->cardType=ATTACK;v=(rand()%5)+1;createdCard->value=v;}是的!这纠正了问题!否则{createdCard->cardType=ATTACK;v=(rand()%5)+1;createdCard->value=v;}是的,这就是问题所在!感谢您的帮助和解释错误。现在我收到一个分段错误(内存转储)??请提出一个新问题是的,这就是问题所在!感谢您的帮助和解释错误。现在我收到一个分段错误(内存转储)??请提出一个新问题