<span style="font-family:Microsoft YaHei;font-size:24px;"></span><pre class="cpp" name="code">#include<stdlib.h>
#include<time.h>
#include<stdio.h>
int menu() //选择菜单函数
{
int choose = 0;
printf("**************************\n");
printf("Please choose:\n");
printf("1.Game\n");
printf("0.EXIT\n");
printf("**************************\n");
while(1)
{
scanf("%d",&choose);
if(1 == choose)
{
return 1;//当选择游戏时返回1
}
else if(0 == choose)
{
return -1;//当选择退出时返回0
}
else
{
printf("Please choose again!\n");//输入其他值时重新输入
}
}
}
void Game() //游戏
{
int num = 0;
int r = 0;
srand( (unsigned)time( NULL ) );//调用srand函数,当时间不同,产生的随机数不同
r = rand()%100; //调用rand函数用于产生随机数
do
{
printf("Please input the number you guess:\n");
scanf("%d",&num);
if(num > r)
{
printf("It is big.\n");
}
else if(num < r)
{
printf("It is small.\n");
}
else
{
printf("Congratulations, you guessed it!\n");
break;
}
}while(1);//循环猜数
}
int main()
{
int ret = 0;
while(1)
{
ret = menu();//调用选择菜单函数
if(1 == ret)
{
Game();
}
else
{
break;
}
}
return 0;
}
如有错误,欢迎指出!