1-100的猜数字游戏
#include <stdlib.h>//srand头文件
#include <time.h>//time头文件
void menu()//菜单
{
printf("1-100猜数字游戏\n");
printf("1、开始\n");
printf("0、结束\n\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\n");
}
else if (guess > ret)
{
printf("大了\n\n");
}
else
{
printf("恭喜,猜对了!\n\n");
break;
}
}
}
int main()
{
int i = 0;
srand((unsigned int)time(NULL));//生成随机数//unsigned int强制类型转换为int//time(NULL)时间戳概念,百度可差
do
{
menu();//打印菜单
printf("请选择:");
scanf("%d", &i);
switch (i)
{
case 1:
printf("开始\n");
game();
break;
case 0:
printf("退出\n");
break;
default:
printf("失败,重新选择\n");
break;
}
} while (i);
return 0;
}