0

当我运行程序时,第一个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);
}
4

2 回答 2

2

改变:

printf("\n\n%d:%s%3c%3d/%d", i+1, ' ',monsterhp[i],monstermaxhp[i]);

printf("\n\n%d: %s   %3d/%d", i+1, monster[i], monsterhp[i], monstermaxhp[i]);

您正在尝试以错误的顺序 printf 。我想你错过了怪物的名字!也不需要使用 %c 的 3 个空格,所以我在格式字符串中内联了空格。

您现在得到如下输出:

1:好 1/1

2:邪恶 2/2

于 2013-10-31T08:03:50.440 回答
0

好的,您的代码有一些问题:

  1. 您正在声明数组:monster,monstermaxhp 和 monsterhp 每次在外部循环时

  2. 它不是return(0);..它的return 0;

  3. 在 for 循环中做后增量(更改++ii++

  4. 将您的 printf 语句修复为:

    printf("\n\n%d:%s%3c%3d/%d", i+1, monsterhp[i], ' ', monstermaxhp[i]);

  5. 也改变:

    scanf("%s", &monster[i]);

    对此:

    scanf("%s", monster[i]);

于 2013-10-31T08:06:29.597 回答