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


    1.认识头文件.

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

    1.1调用system函数的头文件

    system头文件的详细解释

    #include 
    
    • 1

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

    #include 
    
    • 1

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

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

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

    2.主题思路

    2.1代码展示:

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

    #include 
    #include 
    #include 
    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;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    效果展示

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

    3.创作灵感及心得:

    3.1阐述:

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

  • 相关阅读:
    Rouge安装问题
    java计算机毕业设计高校微后勤服务平台源码+mysql数据库+系统+lw文档+部署
    Android Span进阶之路——ClickableSpan
    D. Monocarp and the Set
    KMS在腾讯云的微服务实践助力其降本50%
    前端知识学习03
    库克建议想要侧载的用户购买安卓手机,苹果这是飘了?
    NeRF OpenCV OpenGL COLMAP DeepVoxel坐标系朝向
    关于c语言的switch用法
    羽毛球馆预约小程序 v9.0.1
  • 原文地址:https://blog.csdn.net/qq_69683957/article/details/126152340