本帖最后由 mingcxx 于 2016-10-3 01:34 编辑
有四处错误,做了注释:#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, answer, range, Time = 0; /*number是随机数,answer是用户输入的数字,range为了定义随机数的范围,Time是为了计算用户输入的次数*/
range = 20 - 1;
srand(time(NULL));
number = rand() % range + 1;
printf("I am thinking of a number between 1 and 20.\nCan you guess what it is?");
scanf("%d", &answer);
Time = Time + 1; //1. 第一次
if (answer == number)
{
printf("Congratulations!You did it!\n");
}
else
{
while ((answer != number) && Time < 4)
/*2. while ((answer != number) || (Time < 4))*/
{
if (answer < number)
/*3. if (answer > number)*/
{
printf("Your guess is low. Try again:");
}
else
{
printf("Your guess is high, Try again:");
}
Time = Time + 1;
scanf("%d", &answer);
/*4. scanf("%d", answer);*/
}
if (answer == number)
{
printf("Congratulations!You did it!\n");
}
else
{
printf("Sorry. The number was %d.\nYou should have gotten it by now.\nBetter luck next time.", number);
}
}
return 0;
}
|