美文网首页
demo1:一个猜数游戏

demo1:一个猜数游戏

作者: Bug2Coder | 来源:发表于2019-08-17 18:52 被阅读0次
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<Windows.h>
#include<string.h>    // 字符串变量引用时,需要导入此头文件

#define random(a,b) (rand()%(b-a)+a)
using namespace std;
//猜数游戏,三次机会

// 获取用户名函数
std::string getUsername() {
    const int MAX_LEN = 100;
    char szBuffer[MAX_LEN];
    DWORD len = MAX_LEN;
    if (GetUserName(szBuffer, &len))     //用户名保存在szBuffer中,len是用户名的长度
        return szBuffer;
    else
        return "";
}

int main() {
    std::string ss = getUsername();
    cout << ss << "欢迎您体验猜数游戏!!!" << endl;
    while (true) {
        string cc = "您将有三次机会...";    // 创建字符串变量
        cout << cc << endl;
        int num = 0;
        int count = 3;
        srand((int)time(0));  // 用时间戳产生随机种子
        num = random(1, 10);
        int a = 0;
        for (int i = 0; i < 3; i = i + 1) {
            cout << "请输入一个数字(范围1-10):";
            cin >> a;
            cout << "您输入的数字是:" << a << endl;
            if ( a > num) {
                cout << "数字大了" << "还有次数:" << 2 - i << endl;
            }
            else if (a < num) {
                cout << "数字小了" << "还有次数:" << 2 - i << endl;
            }
            else {
                cout << "恭喜您,猜对了!!!" << endl;
                break;
            }
        }
        cout << "游戏结束,您还接着玩游戏吗?退出请关闭本程序!!!" ;
        system("pause");
        cout << "---------------------------------------------------------------------" << endl;
    }
    
    return 0;

}

相关文章

  • demo1:一个猜数游戏

  • 猜数游戏

    #!/usr/bin/env python #_*_conding:utf-8_*_ age =22 count ...

  • 猜数游戏

    牛牛和羊羊在玩一个有趣的猜数游戏。在这个游戏中,牛牛玩家选择一个正整数,羊羊根据已给的提示猜这个数字。第i个提示是...

  • 猜数游戏

    这个游戏比较适合一二年级及其以下的孩子。 两人每人拿一张卡片,写下1-100之间的任何一个数字,把卡片叠好,互相交...

  • 2019-05-22

    python 猜数游戏

  • Python猜数小游戏

    今天给大家带来一个Python猜数小游戏,游戏十分简单,系统自动生成一个随机数,由玩家猜数,系统提示偏大或偏小,直...

  • 猜数游戏(数独)

    【学习目标】 1.经历填数游戏活动,初步提高分析推理能 力。 2.在探索、尝试、交流等活动中,体会填数 游戏...

  • python猜数游戏

    #!/usr/bin/env python # coding:utf-8 import random j = 3 ...

  • 猜数游戏反思

    这节课还有一些值得反思的地方。1、在让学生用两种等式性质解方程时,对“几X”在等式中的角色强调过多,分散了学生的注...

  • NEUQ-Cpp-03-编程题

    7-1 简单的猜数字游戏[1] (4分) 简单的猜数字游戏是预先设置一个100以内的正整数作为被猜数,用户输入一个...

网友评论

      本文标题:demo1:一个猜数游戏

      本文链接:https://www.haomeiwen.com/subject/gpmksctx.html