#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
int menu() {
printf(" 猜数字游戏 \n");
printf(“\n");
printf(" 1.开始一局游戏.\n");
printf(" 2.退出. \n");
printf("\n”);
int choice = 0;
scanf("%d", &choice);
return choice;
}
void game() {
system(“cls”);
int to_guess = rand() % 100;//自动生成一个随机数(0-65535)
while (1) {
printf(“请输入一个随机数:”);
int end;
scanf("%d", &end);
if (end == to_guess)
{
system(“cls”);
printf(“恭喜你猜对了.\n”);
Sleep(3000);
system(“cls”);
break;
}
else if (end > to_guess)
{
system(“cls”);
printf(“您猜大了.\n”);
}
else
{
system(“cls”);
printf(“您猜小了.\n”);
}
}
}
int main()
{
srand(time(0));//把当前时间设为随机种子
while (1) {
int choice=menu();
if (choice == 1) {
system(“cls”);
game();
}
else if (choice == 2)
{
system(“cls”);
printf(“goodbye”);
break;
}
else {
system(“cls”);
printf(“您的输入有误.”);
}
}
system(“pause”);
return 0;
}
C语言实现猜数字游戏
最新推荐文章于 2022-10-16 16:56:08 发布