void menu()
{
printf("##############################\n");
printf("####### 1. play #########\n");
printf("####### 0. exit #########\n");
printf("##############################\n");
}
void game()
{
//0x7fff
//0111 1111 1111 1111
//RAND_MAX
//1.生成随机数
int num = rand() % 100 + 1;;
int guess = 0;
//printf("%d\n", num);
//2.猜数字
//printf("猜数字\n
while (1)
{
printf("请猜数字(1-100)>:");
scanf("%d", &guess);
if (guess < num)
{
printf("猜小了\n");
}
else if (guess > num)
{
printf("猜大了\n");
}
else
{
printf("猜对了\n");
break;
}
}
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));//time_t == unsigned int
do
{
menu();
printf("请选择》:");
scanf("%d", &input);
switch(input)
{
case 1:
game();
break;
case 0:
break;
default:
printf("请重新输入\n");
break;
}
}while(input);
return 0;
}
耗时两个小时才把这个游戏做出来,但是还是蛮有成就感的。