猜数字游戏(两种实现方式)
vs2019编译环境
#include
#include
#include
void menu()
{
printf("*****************************n"
"********进入1**退出0*********n"
"*****************************n");
}
void game()
{
int ret;
int guess;
srand((unsigned int)time(NULL));//时间戳 NULL空指针
ret = rand() % 100 + 1;
//printf("%d", ret);//数显game函数方案一
while (1)//实现game函数方案二
{
printf("请输入你猜想的数字");
scanf_s("%d", &guess);
if (guess > ret)
{
printf("猜大了n");
}
else if (guess < ret)
{
printf("猜小了n");
}
else
{
printf("恭喜你猜对了n");
break;
}
}
}
int main()
{
int input;
printf(" 欢迎来到猜数字 n");
menu();
printf("请输入1/0n");
scanf_s("%d", &input);
if (input==1)
{
game();
}
else if (input == 0)
{
printf("游戏结束n");
}
else
printf("输入错误n");
return 0;
}



