C语言猜数字游戏(数字范围1-100)
程序员文章站
2022-03-27 17:03:48
#define _CRT_SECURE_NO_WARNINGS 1#include#include#includevoid menu(){printf("******************************\n");printf("***** 0.exit 1.play *****\n");printf("******************************\n");...
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("******************************\n");
printf("***** 0.exit 1.play *****\n");
printf("******************************\n");
}
void play()
{
printf("您已经进入游戏\n");
int res = rand() % 100 + 1;
int num = 0;
while (1)
{
printf("请输入一个数字");
scanf("%d", &num);
if (num == res)
{
printf("恭喜你猜对了\n");
break;
}
else if (num > res)
printf("您输入的数字偏大\n");
else
printf("您输入的数字偏小\n");
}
}
void game_over()
{
printf("退出游戏");
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请输入数字开始游戏\n");
scanf("%d",&input);
switch (input)
{
case 1:
{
play();
break;
}
case 0:
{
game_over();
break;
}
default:
{
printf("请您重新选择\n");
break;
}
}
} while (input);
return 0;
}
本文地址:https://blog.csdn.net/qq_45767476/article/details/107875328