用C语言编写一个具有简单界面的猜数字游戏
发布网友
发布时间:2023-11-04 21:30
我来回答
共2个回答
热心网友
时间:2024-07-01 15:44
分析:
先产生一个随机数N。
然后输入数I,如果i大于N,则提示大于信息。
如果I小于N,则提示小于信息。
直到I==N,则输出成功信息。
这是我用C语言写的。
环境:
WIN-C ,TORBO C,如果是C++环境把倒数第二排getch();删掉!
已经调试成功:
main()
{
int i=0,n;
srand(time(0));
n=rand()%100+1;
while(i!=n)
{printf("please input a number:\n");
scanf("%d",&i);
if(i>n)printf("this number is too big!\n");
if(i<n)printf("this number is too smaller!\n");
}
if(i==n)
printf("PASS!%3d",n);
getch();
}
提示:
srand(time(0));
n=rand()%100+1;
是用来生成一个1~100以内的随机数,如果你改,把100改成50或者200。如(n=rand()%50+1;
)
热心网友
时间:2024-07-01 15:44
y#include "time.h"
#include "stdlib.h"
#include "stdio.h"
main()
{char c;
clock_t start,end;
time_t a,b;
double var;
int i,guess;
srand(time(NULL));
printf("do you want to play it.('y' or 'n') \n");
loop:
while((c=getchar())=='y')
{
i=rand()%100;
printf("\nplease input number you guess:\n");
start=clock();
a=time(NULL);
scanf("%d",&guess);
while(guess!=i)
{if(guess>i)
{printf("please input a little smaller.\n");
scanf("%d",&guess);}
else
{printf("please input a little bigger.\n");
scanf("%d",&guess);}
}
end=clock();
b=time(NULL);
printf("\1: It took you %6.3f seconds\n",var=(double)(end-start)/18.2);
printf("\1: it took you %6.3f seconds\n\n",difftime(b,a));
if(var<15)
printf("\1\1 You are very clever! \1\1\n\n");
else if(var<25)
printf("\1\1 you are normal! \1\1\n\n");
else
printf("\1\1 you are stupid! \1\1\n\n");
printf("\1\1 Congralations \1\1\n\n");
printf("The number you guess is %d",i);
}
printf("\ndo you want to try it again?(\"yy\".or.\"n\")\n");
if((c=getchar())=='y')
goto loop;
system("pause");
return 0;
}