1、电脑生成一个随机数
2、猜数字
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
void menu() {
printf("******************************");
printf("*** 1.PLAY 0.EXIT *****");
printf("******************************");
}
void game() {
int rec;
int guess;
rec = rand() % 100 + 1;
while (1) {
printf("请猜数字:\n");
scanf("%d", &guess);
if (guess > rec) {
printf("猜大了\n");
}
else if (guess < rec) {
printf("猜小了\n");
}
else {
printf("恭喜你猜对了!\n");
break;
}
}
}
int main()
{
int input;
srand((unsigned int)time(NULL));
do {
menu();
printf("请选择:\n");
scanf("%d", &input);
switch (input) {
case 1:
game();
break;
case 0:
printf("退出游戏\n");
break;
default:
printf("选择错误\n");
break;
}
} while (input);
return 0;
}
标签:guess,游戏,int,input,break,printf,rec,数字
From: https://blog.51cto.com/u_16080683/6252339