小弟不才,第一次写猜数游戏 = =!
/*****************************************************************
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<cstdio>
#include<cstdlib>
#include<ctime>
int count=15;
char IsOk(int randnum,int input)
{
char ch='Y';
switch((int)(randnum<input))
{
case 1: printf("太大了\n"); break;
case 0: switch((int)(randnum>input))
{
case 1: printf("太小了\n"); break;
case 0: printf("恭喜你,猜对了!\n");
printf("是否继续游戏?(Y/N)");
scanf(" %c",&ch);
count=15;
break;
}
break;
}
return ch;
}
int GetRandNum()
{
int num;
srand((unsigned int)time(0));
for(num=rand();num<=0||num>=1000;num=rand());
return num;
}
int main()
{
int num,rand_num;
char ch='Y';
printf("你有 15 次猜数机会!猜数范围在[1,1000]\n");
printf("下面开始猜数游戏,请输入你要猜的数字...\n");
rand_num=GetRandNum();
while((ch=='Y')&&(scanf("%d",&num),num>0))
{
if((ch=IsOk(rand_num,num))=='Y'&&count==15)
rand_num=GetRandNum();
if(--count == 0)
{
printf("你的次数已用完,你输了- -!\n");
printf("是否重新来过?(Y/N)");
scanf(" %c",&ch);
rand_num=GetRandNum();
count=15;
}
}
return 0;
}
** HighlightCodeV3.0 software by yzfy(雨中飞燕) http:// **
*****************************************************************/
#include<cstdio>
#include<cstdlib>
#include<ctime>
int count=15;
char IsOk(int randnum,int input)
{
char ch='Y';
switch((int)(randnum<input))
{
case 1: printf("太大了\n"); break;
case 0: switch((int)(randnum>input))
{
case 1: printf("太小了\n"); break;
case 0: printf("恭喜你,猜对了!\n");
printf("是否继续游戏?(Y/N)");
scanf(" %c",&ch);
count=15;
break;
}
break;
}
return ch;
}
int GetRandNum()
{
int num;
srand((unsigned int)time(0));
for(num=rand();num<=0||num>=1000;num=rand());
return num;
}
int main()
{
int num,rand_num;
char ch='Y';
printf("你有 15 次猜数机会!猜数范围在[1,1000]\n");
printf("下面开始猜数游戏,请输入你要猜的数字...\n");
rand_num=GetRandNum();
while((ch=='Y')&&(scanf("%d",&num),num>0))
{
if((ch=IsOk(rand_num,num))=='Y'&&count==15)
rand_num=GetRandNum();
if(--count == 0)
{
printf("你的次数已用完,你输了- -!\n");
printf("是否重新来过?(Y/N)");
scanf(" %c",&ch);
rand_num=GetRandNum();
count=15;
}
}
return 0;
}
[[it] 本帖最后由 中学者 于 2008-7-4 21:44 编辑 [/it]]