#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void menu()
{
printf("***********************\n");
printf("*** 1.play 0.exit ***\n");
printf("***********************\n");
}
void game()
{
int guess = 0;
int h_numb = 0;
h_numb = rand()%100+1;
//RAND MAX - 32767
//利用rand()函数生成随机数(伪随机数 pseudo-random integer)
//rand() % 100 在0-99之间,+1变成 0-100;
printf(">>>>>>GUESS NUMBER<<<<<<\n");
printf("what do you think the hidden number is?\n");
while (1)
{
scanf("%d",&guess);
if (guess > h_numb)
{
printf("Make it less\n");
}
else if (guess < h_numb)
{
printf("Make it greater\n");
}
else
{
printf("You got it!\n");
break;
}
}
}
int main()
{
int num = 0;
srand((unsigned int)time(NULL));
//利用时间戳设置随机数生成的种子点
do
{
menu();
printf("Select a number:");
scanf("%d",&num);
switch (num)
{
case 1:
game();
break;
case 0:
printf("Game over!\n");
break;
default:
printf("Select again!\n");
break;
}
}
while (num);
return 0;
}
习题 猜数字
最新推荐文章于 2023-11-11 18:27:48 发布