猜数字游戏
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
//猜数字游戏
//1.生成一个随机数
//2.猜这个随机数
void menu()
{
printf("++++++++++++++++++++++++++++++++++\n");
printf("+ +\n");
printf("+ 1:开始游戏 2:结束游戏 +\n");
printf("+ +\n");
printf("++++++++++++++++++++++++++++++++++\n");
}
void game()
{
int b = 0;
int a;
srand((unsigned int)time(NULL));//time()是时间戳函数,与计算机时间有关
a=rand()%100+1;//rand()是生成随机数的函数,默认范围是0-32767。让其除以100取余数来生成0-100的随机数
int guess = 1;
while (1)
{
printf("请猜一个0-100内的数字->");
scanf("%d", &guess);
if (guess > a)
{
printf("猜大了\n");
}
else if (guess < a)
{
printf("猜小了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
do
{
menu();//菜单函数
printf("请选择->\n");
scanf("%d", &input);
switch (input)
{
case 1:
game();//游戏函数
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("请选择正确的序号!!!!!1\n");
break;
}
}
while (input != 2);
return 0;
}