void menu()
{
printf("------猜数字------\n");
printf("******************\n");
printf("***** 1.play *****\n");
printf("***** 0.exid *****\n");
printf("******************\n");
printf("------------------\n");
}
void game()
{
//产生随机数
int ret = rand()%100+1;//%100的余数是0-99,然后+1,就是1-100;
//猜数字
int guess = 0;
while (1)
{
printf("请猜数字:>");
scanf("%d", &guess);
if (guess < ret)
{
printf("猜小了\n");
}
else if (guess>ret)
{
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:
break;
default:
printf("输入错误,请重新输入\n");
break;
}
} while(input);
return 0;
}
标签:guess,游戏,int,break,printf,input,100,数字
From: https://blog.csdn.net/yuan_wanqiang/article/details/139393196