用C语言编写一个具有简单界面的猜数字游戏
答案:3 悬赏:0 手机版
解决时间 2021-03-04 22:32
- 提问者网友:流星是天使的眼泪
- 2021-03-04 14:25
功能要求:计算机产生随机数,猜中即胜,猜不中提示是大了还是小了,继续猜,直至猜到,给出所用时间和评语.
最佳答案
- 五星知识达人网友:罪歌
- 2021-03-04 15:33
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 Congradulations \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;
}
#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 Congradulations \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;
}
全部回答
- 1楼网友:七十二街
- 2021-03-04 16:10
分析:
先产生一个随机数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;
)
- 2楼网友:躲不过心动
- 2021-03-04 16:05
同学你好!
下面是我自己做了一个,运行是成功的。希望对你有所帮助!
#include
#include
#include
#include
void main()
{
int i,j,cnt,guard=1;
while(guard)
{
srand((unsigned)time(null));
i=rand()%100;
cnt=0;
for(;;)
{
printf("please input the data:");
scanf("%d",&j);
cnt++;
if(j>i)
printf("too high!\n");
else if(j
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯