'如果'在'For'内部崩溃游戏 - C语言

时间:2013-11-22 16:04:20

标签: c

这是一个代码:(whyerror1.exe)

#include <stdio.h>
#include <stdlib.h>
int i, n, health=100;
int main(void)
{
    for (i=0; i<1; i++)
    {
        printf("health: %d\n",health);
        printf("There is meat on the ground.\n");
        printf("Will you eat it?\n");
        printf("1: yes  2: no\n"); 
        scanf("%d", n);
        if(n==1) {
                      system("cls");
                      printf("Ate it. \n");
                      printf("There is no people, so it is safe to eat. \n");
                      printf("Health is increased.\n");
                      system("pause>nul");
                      system("cls");
                      }
        else if(n==2) {
                      system("cls");
                      printf("Didn't ate it. \n");
                      printf("More hunger, less health. \n");
                      system("pause>nul");
                      system("cls");
                      }
        else {
                     printf("select between 1 and 2. \n");
                     system("pause>nul");
                     system("cls");
                     i=-1;
        }
    }
}

结果:whyerror1.exe停止工作。 为什么会这样? 我已经尝试过选择其他数字了。我试过任何数字! Plz帮忙!

1 个答案:

答案 0 :(得分:4)

scanf("%d", n);

应该是:

scanf("%d", &n);