求指出问题在哪里,我提交后系统显示格式错误。
令游戏机随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,则提示“Game Over”,并结束程序。如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序
#include <stdio.h>
int main()
{
int number,times,guess,i=0;
scanf("%d %d %d",&number,×,&guess);
while(guess>0)
{
i++;//计数猜的次数
if(i<=times)//规定次数以内的情况
{
if(guess>number){printf("Too big");}//猜得过大
else if(guess<number){printf("Too small");}//猜得过小
else//猜中了,分情况
{
if(i==1){printf("Bingo!");}//一次猜中
else if(i>1&&i<=3){printf("Lucky You!");}//3次以内猜中
else{printf("Good Guess!");}//3次到规定次数以内猜中
return 0;//猜中了,退出程序
}
}
else{printf("Game Over");return 0;}//超过规定次数,退出程序
scanf("%d",&guess);
}
printf("Game Over");//输入负数且没有猜中,退出程序
return 0;
}
你的逻辑错了,猜的次数不需要输入
代码修改如下:
#include <stdio.h>
#include <time.h>
#define N 5 //定义猜的次数
int main()
{
int num,times=0,guess;
srand((unsigned)time(NULL));
num = rand()%100; //生成100以内的随机数
while(1)
{
scanf("%d",&guess);
times++;
if(times <=N && guess < 0) //输入负数结束循环
break;
if(times > N) break; //超出N此后,结束循环
if(guess > num)
printf("Too big\n");
else if(guess < num)
printf("Too small");
else
break;
}
if(times == 1)
printf("Bingo!\n");
else if(times >1 && times <=3)
printf("Lucky You\n");
else if(times > 3 && times <=N)
printf("Good Guess\n");
else
printf("Game Over\n");
return 0;
}