//均已调试通过,编译器为DEV C++
//猜数游戏程序 01-10-07 17:46
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main()
{
int num,r,i=0;
char ch;
Start:
i=0;
srand((unsigned )time(NULL)); //给srand()提供一个种子 。
r=rand()%1000+1; //取余产生1--1000的随机数。
printf("%d/n",r);
printf("I have a number between 1 and 1000,Can you guess the number?/n");
Begin:
printf("you have %d time !",10-i); //提示剩余次数
printf(" please type you guess :/n");
i++;
if(scanf("%d",&num)!=1) //利用scanf()的返回值,若输入整型返回1,否则返回0
{
fflush(stdin); //清空键盘输入缓冲区 ,包括上次输入的回车
//在接受前最好先清除缓冲区
printf("Error,please input num between 1--1000./n");
goto Begin;
}
if(num<=1||num>=1000)
{
printf("Error,please input num between 1--1000./n");
goto Begin;
}
if(i>=10)
{
printf("Too bad!!!Bye bye./n");
goto End;
}
else if(num==r)
{
printf("Excellent!You guessed the number!/n");
printf("Would you like to play again(y or n)?/n");
fflush(stdin); //清除键盘缓冲区的内容
if( ( ch=getchar())=='y') //注意不同的引号 ,接受字母的方法 以及表示格式
// if(ch=='y')
goto Start;
else if(ch=='n')
goto End;
}
else if(num<r)
{
printf("Too low,Try again./n");
goto Begin;
}
else if(num>r)
{
printf("Too high,T0ry again./n");
goto Begin;
}
End:
return 0;
}
//猜数游戏程序 01-10-07 17:46
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
int main()
{
int num,r,i=0;
char ch;
Start:
i=0;
srand((unsigned )time(NULL)); //给srand()提供一个种子 。
r=rand()%1000+1; //取余产生1--1000的随机数。
printf("%d/n",r);
printf("I have a number between 1 and 1000,Can you guess the number?/n");
Begin:
printf("you have %d time !",10-i); //提示剩余次数
printf(" please type you guess :/n");
i++;
if(scanf("%d",&num)!=1) //利用scanf()的返回值,若输入整型返回1,否则返回0
{
fflush(stdin); //清空键盘输入缓冲区 ,包括上次输入的回车
//在接受前最好先清除缓冲区
printf("Error,please input num between 1--1000./n");
goto Begin;
}
if(num<=1||num>=1000)
{
printf("Error,please input num between 1--1000./n");
goto Begin;
}
if(i>=10)
{
printf("Too bad!!!Bye bye./n");
goto End;
}
else if(num==r)
{
printf("Excellent!You guessed the number!/n");
printf("Would you like to play again(y or n)?/n");
fflush(stdin); //清除键盘缓冲区的内容
if( ( ch=getchar())=='y') //注意不同的引号 ,接受字母的方法 以及表示格式
// if(ch=='y')
goto Start;
else if(ch=='n')
goto End;
}
else if(num<r)
{
printf("Too low,Try again./n");
goto Begin;
}
else if(num>r)
{
printf("Too high,T0ry again./n");
goto Begin;
}
End:
return 0;
}