10.【C++猜数字游戏(看一眼就会)】

发布于:2023-01-31 ⋅ 阅读:(599) ⋅ 点赞:(0)

1.认识头文件.

定义:头文件是指可以比作某些特殊函数的老大,没有老大出面、小跟班自然不敢撒野!

1.1调用system函数的头文件

system头文件的详细解释

#include <stdlib.h>

1.2调用srand()函数的头文件

#include <ctime>

srand()函数是一个种子、为rand()函数铺路用的.它们两个成对出现.

srand((int)time(NULL));     //  设置一个随机数的种子、以目前时间为变化(默认为五位数)
	n = rand() % 100 + 1;      //目的是为把数字控制字在1-100之间;

srand((int)time(NULL))、为固定格式

2.主题思路

2.1代码展示:

运用变量进行随机生成一个数字、然后我们手动输入判断是否相等、相等胜利、反之失败!

#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main()
{
	cout << "***********************欢迎来到猜数字小游戏***********************" << endl << endl << endl;
	cout << "游戏规则:" << "猜到系统给的随机数字就能获胜、反之失败!一次只有10次机会." << endl;
	system("color 70");            //改变颜色
	int n,m,j=0;
	srand((int)time(NULL));     //  设置一个随机数的种子、以目前时间为变化(默认为五位数)
	n = rand() % 100 + 1;      //目的是为把数字控制字在1-100之间;
	cout <<"请您在1-100之间输入一个数字" << endl;
	cin >> m;
	while (m != n)          //判断您输入的数是否和随机数相等
	{
		if (m < n)
		{
			j++;
			system("cls");
			cout << "您输入的数小了" << endl;
			cout << "系统已自动为您缩小范围:" << endl;
			cout << "****************还剩下" << 10 - j << "次机会****************" << endl;
			cout << m << "-" << 100<< endl;
			
			system("color E0");
		}
		else
		{
			j++;
			system("cls");
			cout << "您输入的数大了" << endl;
			cout << "系统已自动为您缩小范围:" << endl;
			cout << "****************还剩下" << 10 - j << "次机会****************" << endl;
			cout << 0 << "-" << m << endl;
			system("color 80");
		}
		if (j == 10)
		{
			system("cls");
			cout << endl<<endl<<endl<<endl<<"次数已用尽闯关失败!" << endl << endl << endl << endl;
			system("color 40");
			cout << "**************************游戏结束**************************" << endl;
			system("pause");
		}
		cin >> m;
	}
	cout << "恭喜您闯关成功" << endl << endl << endl;
	cout << "**************************游戏结束**************************" << endl;
	return 0;

}

效果展示

在这里插入图片描述
在这里插入图片描述

3.创作灵感及心得:

3.1阐述:

本次游戏创作灵感来源于CSDN、我把它也奉献与CSDN!
我们并肩作战、创造辉煌!

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到