C语言实现猜数字游戏


<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;
}

 


       如有错误,欢迎指出!



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值