当我运行程序时,第一个for
循环工作得很好,但是"Program has stopped working"
在进入第二个while
循环之前我得到了消息。我的编码是否有错误,如果有,我该如何解决?
#include<stdio.h>
double Combat(int x, int y, char mons[20]);
int main(void)
{
int monsters, i, target, alive;
while(1)
{
printf("ENTERING COMBAT\n\nHow many monsters?> ");
scanf(" %d", &monsters);
char monster[monsters][20];
int monstermaxhp[monsters];
int monsterhp[monsters];
for(i=0;i<monsters;++i)
{
printf("\n\nNO SPACES\n\nEnter Monster %d's name> ", i+1);
scanf("%s", &monster[i]);
printf("\n\nEnter %s's hitpoints> ", monster[i]);
scanf("%d", &monstermaxhp[i]);
monsterhp[i]=monstermaxhp[i];
}
alive=1;
while(alive==1)
{
for(i=0;i<monsters;++i)
{
printf("\n\n%d:%s%3c%3d/%d", i+1, ' ',monsterhp[i],monstermaxhp[i]);
}
printf("\n\nSelect Target> ");
scanf("%d", &target);
i=target-1;
monsterhp[i]=Combat(monsterhp[i],monstermaxhp[i],monster[i]);
for(i=0;i<monsters;++i)
{
if(monsterhp[i]<=0)
alive=0;
}
}
}
return(0);
}
double Combat(int x, int y, char mons[20])
{
int damage, plrroll, monroll;
printf("\nRoll for %s> ", mons);
scanf("%d", &monroll);
printf("\nRoll for Player> ");
scanf("%d", &plrroll);
if(plrroll>monroll)
{
printf("\nHIT! Roll for damage> ");
scanf("%d", &damage);
x=x-damage;
}
else
{
printf("\nMISS! :P\n");
}
return(x);
}