题目:设计游戏界面,生成一个1~100以内的随机数,用户输入所猜想的数字,如果所猜的数字大于该随机数,输出“你猜大了”;如果所猜的数字小于该随机数,输出“你猜小了”;如果所猜的数字等于该随机数,输出“恭喜你猜对了!”;用户有5次猜数字的机会,如果五次机会都用光了也没有猜出该随机数,输出“你输了!”。
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int choice;
printf("**************************\n");
printf("****欢迎进入猜数字游戏****\n");
printf("****1.开始游戏 0.退出****\n");
printf("**************************\n");
printf("请输入你的选择:");
scanf("%d",&choice);
int counter=0;
int ret=0;
srand((unsigned int)time(NULL));
ret = rand()%100;
while(1)
{
int count=0;
printf("请输入你猜的数字:");
scanf("%d",&count);
counter++;
if (count==ret)
{
printf("恭喜你猜对了!\n");
break;
}
if (counter==5)
{
printf("你输了!");
break;
}
if(count<ret)
{
printf("你猜小了\n");
}
else
{
printf("你猜大了\n");
}
}
return 0;
}