//******************************猜数字游戏******************************************
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("*********************************************\n");
printf("******* 1、开始游戏 0、退出游戏 *******\n");
printf("*********************************************\n");
}
void game()
{
//生成随机数**********
//RAND_MAX——32767(rand生成的伪随机数在0到32767之间)
int ret = 0;
int guess = 0;
//拿时间戳设置随机数起点
ret=rand()%100+1;
//printf("%d\n", ret);
//猜数字***************
while (1)
{
printf("请猜数字(1~100):\n");
scanf("%d", &guess);
if (guess>ret)
{
printf("猜大了\n");
}
else if (guess<ret)
{
printf("猜小了\n");
}
else
{
printf("恭喜猜中了!");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择(1/0):\n");
scanf_s("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("请重新输入:\n");
break;
}
} while (input);
return 0;
}
猜数字游戏(随机数生成的方法)
最新推荐文章于 2024-05-01 22:13:13 发布