#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("*******猜猜数字吧!*******\n");
printf("********1开始游戏*********\n");
printf("********0退出游戏*********\n");
}
void game()
{
int guess = 0;
int ret = rand() % 100 + 1;//%100求余数
while (1)
{
printf("猜猜数字吧:");
scanf("%d", &guess);
if (guess > ret)
{
printf("猜大了!");
}
else if (guess < ret)
{
printf("猜小了!");
}
else
{
printf("恭喜你猜对了!");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//调用的随机数 必须在循环外面
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("退出游戏!");
break;
default:
break;
}
} while (input);
return 0;
}
|