int main(void)
{
const char * attributeNames = "StrDexConIntWisCha";
int characterValues[7] = {0};
int characterBonuses[7] = {0};
characterStats(characterValues);
}
void characterStats(int * characterValues)
{
int numberOfDice = 4; int diceType = 6;
int x = 1;//because characterValues[0] is level.
printf("What is your level? > ");
scanf("%d",&characterValues[0]);
printf("Current Level [%d]", characterValues[0]);
printf("Rolling stats.\n");
for(x; x <= numberOfDice; x++)
{
characterValues[x] = diceRoll(diceType);//rolling a d6
}
}
int diceRoll(int diceType)
{
int numberOfDice = 4;
int x,y,z = 0;
int diceResult, finalValue, lowestResult = 0;
int diceRoll[4] = {0};
for(x; x <= numberOfDice; x++)
{
printf("%d", diceRoll[x]);
}
}
我正在尝试为地牢和龙的角色生成器创建一个能够滚动6面骰子4次的功能。 diceRoll
中的最后一个for循环似乎没有执行,它只是跳过它而我不明白为什么。目前,我只是在添加rand()
之前测试是否一切正常。
答案 0 :(得分:3)
int x,y,z = 0;
在这里,您只将z
初始化为0
,保留x
和y
未初始化,然后在for
循环中
for(x; x <= numberOfDice; x++)
同样,x
未初始化。
答案 1 :(得分:2)
您应该始终在for
循环中初始化循环计数器:
for(x=0; x<numberOfDice; x++)
也是,请注意我使用<
而不是<=
;从0开始的循环将始终从0 <n
进行 n 次迭代(使用<=
将执行 n + 1 )。从0到 n-1 的循环在所有类C语言中都是惯用的,因为它倾向于匹配索引,迭代的数组或指针操作,这些都是从0开始的。
答案 2 :(得分:-1)
要么
int x=0,y,z=0;
或制作
for(x = 0; x <= numberOfDice; x++)