#include <stdio.h>
#include <stdlib.h>
#include<time.h>
void game() {
int r=rand()%100+1;
int guess=0;
int count = 8;
while (1) {
printf("请猜一个数字:");
scanf_s("%d", &guess);
if (guess == r) {
printf("恭喜你,猜对了!\n");
break;
}
else if (guess > r) {
printf("猜大了,请重新输入!\n");
}
else {
printf("猜小了,请重新输入!\n");
}
count--;
printf("你还有%d次机会!\n", count);
if (count == 0) {
printf("你已经用完了8次机会,你已失败,游戏结束!\n");
break;
}
}
}
void menu() {
printf("***************************\n");
printf("******* 1. 开始游戏 *******\n");
printf("******* 2. 退出游戏 *******\n");
printf("***************************\n");
}
int main() {
int input;
srand((unsigned int)time(NULL));
do {
menu();
printf("请输入你的选择:");
scanf_s("%d", &input);
switch (input) {
case 1:
game();
break;
case 2:
printf("退出游戏\n");
break;
default:
printf("输入错误,请重新输入!\n");
break;
}
} while (input);
return 0;
}