猜数字小游戏

来源:百度知道 编辑:UC知道 时间:2024/06/28 07:11:26
该C程序有个问题:不能限制猜的次数,想请各位大大帮忙改下,使程序运行过程中如果你猜测次数超过你所规定的范围,就会报错
#include<stdlib.h>
#include<conio.h>
#include<time.h>
#include<stdio.h>
void main()
{ srand((unsigned)time(NULL));
int magic;
int guess;
int counter;
magic=rand()%100+1;
counter=0;
do{
printf("please guesss a magic number(0到100之间):");
scanf("%d",&guess);
counter++;
if (guess>magic)
printf("wrong!too high!你怎么这么苯,猪脑啊\n");
else if(guess<magic)
printf("wrong!too low!我对你失去信心了\n");
}while(guess!=magic);
printf("right!终于猜对了\n");
printf("\t* *\n");
printf("\t *\n");
printf("counter=%d\n",counter);
}

{
int Number = 2;
srand((unsigned)time(NULL));
int magic;
int guess;
int counter;
magic=rand()%100+1;
counter=0;
do{
printf("please guesss a magic number(0到100之间):");
scanf("%d",&guess);
counter++;
if (guess>magic)
printf("wrong!too high!你怎么这么苯,猪脑啊\n");
else if(guess<magic)
printf("wrong!too low!我对你失去信心了\n");
if(counter>=Number)break;
}
while(guess!=magic);
if (counter>=Number) printf("失败!超过最多猜题次数了!\n");
else
{
printf("right!终于猜对了\n");
printf("\t* *\n");
printf("\t *\n");
printf("counter=%d\n",counter);
}
}

用for循环啊!