void menu() //猜数字的代码
{
printf("***********************************\n");
printf("******** 1.play *************\n");
printf("******** 0.exit *************\n");
printf("***********************************\n");
}
void game()
{
int a = 0;
int guess = 0;
int ret = rand() % 100 + 1;//rand 生产随机数的函数
//printf("%d\n", ret);
for (a = 0; a <= 3; a++)
{
printf("请猜数字:");
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess > ret)
{
printf("猜大了\n");
}
else
{
printf("猜对了\n");
break;
}
}
}
int main()
{
srand((unsigned int)time(NULL));//NULL是空指针的意思 相当于0 srand 是设置随机数的起点
int input = 0;
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input) // switch 列举
{
case 1:
printf("猜数字");
game();
break;
case 0:
printf("退出游戏");
break;
default:
printf("选择错误,请重选");
break;
}
} while (input);
return 0;
}