简单的猜数字游戏

功能简介

玩家输入一个数字,提示高了或者低了或者猜对了

主要思路

首先需要一个菜单,提示我们进行操作(开始游戏或者结束游戏)

自动生成一个随机数:scrand(time(0))使用当前时间作为一个随机种子;程序自动地生成一个1~100的随机数 rand() % 100 + 1(关于rand详情可在cplusplus网站中查看)

采用while(1)死循环,判断玩家输入的数字

代码示例

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<windows.h>

//菜单函数
int Menue(){
	int choice;
	printf("****************************\n");
	printf("欢迎来到猜数字游戏,请输入你的选择\n");
	printf("1.开始游戏\n2.退出游戏\n");
	printf("****************************\n");
	scanf("%d", &choice);
	return choice;
}
//游戏
void game()
{
	//程序自动地生成一个1~100的随机数
	int toGuess = rand() % 100 + 1;
	while (1){
		int input = 0;
		printf("请输入你所要猜测的数字:\n");
		scanf("%d", &input);
		if (input<toGuess){
		printf("低了!\n");
	}
		else if (input>toGuess){
		printf("高了!\n");
	}
	else{
		printf("恭喜你!猜对了!!\n");
        //猜对时跳出循环
		break;
	}
  }
}
int main()
{
	//scrand使用当前时间作为一个随机种子
	srand(time(0));
	while (1){
		int choice = Menue();
		if (choice == 1){
			game();
		}
		else if (choice == 2){
			printf("退出程序!再见!\n");
			break;
		}
		else{
			printf("请输入提示的数字\n");
		}
	}
	system("pause");
	return 0;
}
 

运行结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值