当前位置: 动力学知识库 > 问答 > 编程问答 >

Only last element of C array saving

问题描述:

I'm pretty new to C, and I was screwing around with some things that we were doing in class. But I've run across a problem:

srand(time(NULL));

for (counter = 0; counter < 7; counter = counter + 1);

{

stats[counter] = abs(rand() % max);

}

When I go to print these elements, however, only the last one makes any sense, everything else is -858993460

printf("Health: %d\n", stats[0]);

printf("Armor: %d\n", stats[1]);

printf("Damage: %d\n", stats[2]);

printf("Critical Strike Shance: %d\n", stats[3]);

printf("Critical Strike Damage: %d\n", stats[4]);

printf("Dexterity: %d\n", stats[5]);

printf("Strength: %d\n", stats[6]);

printf("Luck: %d\n", stats[7]);

Can anyone help?

Thanks!

网友答案:

You have a semi-colon at the end of your for loop line.

for (counter = 0; counter < 7; counter = counter + 1);
                                                     ^^^^

that makes the following block afterwards execute only once. as it is not in the for loop.

分享给朋友:
您可能感兴趣的文章:
随机阅读: