#include<stdio.h>#
#include<stdlib.h>
#include<time.h>
//定义游戏流程和规则
void game()
{
int r = rand() % 100 + 1;//定义一个随机数:1-100
int guess = 0;//用来放你输入的猜的那个数
int count = 8;
while (count)//for while do while
{
printf("你还有%d次机会", count);
printf("请猜数字:\n");
scanf("%d", &guess);
if (guess > r)
{
printf("猜大了\n");
}
else if (guess < r)
{
printf("猜小了\n");
}
else
{
printf("猜对了,恭喜你\n");
break;
}
count--;
}
if (count == 0)
{
printf("8次机会已经用完吗,游戏失败,正确答案是:%d",r);
}
}
//定义游戏菜单栏
void menu()
{
printf("**************************\n");
printf("******** 1.play **********\n");
printf("******** 0.exit **********\n");
printf("**************************\n");
}
//定义游戏循环
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 0:
printf("游戏结束\n");
break;
default:
printf("选择错误,重新选择\n");
break;
}
} while (input);
return 0;
}
分支与循环小游戏,猜数字游戏
最新推荐文章于 2024-09-02 16:22:59 发布