//无限制次数版
#include<stdlib.h>
#include<time.h>
void menu()
{
printf("****************\n");
printf("****欢迎游玩****\n");
printf("***1:开始游戏***\n");
printf("*****0:退出*****\n");
}
void game()
{
int guess = 0;
int a = rand() % 100 + 1;
while (1)
{
printf("请输入一个数:");
scanf("%d",&guess);
if (a < guess)
{
printf("猜大了\n");
}
else if (a > guess)
{
printf("猜小了\n");
}
else
{
printf("猜对了\n");
break;
}
}
}
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("exit\n");
break;
default:
printf("输入有误请重新输入\n");
break;
}
}
while (input);
return 0;
}
(限制次数)第二段代码有些许变更
void game()
{
int guess = 0;
int a = rand() % 100 + 1;
int count = 10;
while (count)
{
printf("你还剩%d次机会\n", count);
printf("请输入一个数:");
scanf("%d",&guess);
if (a < guess)
{
printf("猜大了\n");
}
else if (a > guess)
{
printf("猜小了\n");
}
else
{
printf("猜对了\n");
break;
}
count--;
}
if (count == 0);
{
printf("游戏结束下次再来\n");
}
}