跳到正文

休闲·娱乐

C++猜数游戏

mz99约 1 分钟
原文
文章信息与版本

文章信息

收录
2026年7月2日
更新
2026年8月12日
篇幅
约 1 分钟

版本

  1. v1当前

豆包挺好用的,但还是自己写的玩的更带劲,也不知到干什么……

C++
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    srand((unsigned)time(NULL));
    while(true)
    {
        int ans = rand()%100 + 1;
        int g,cnt=0;
        cout << "===== 猜数游戏 1-100 =====" << endl;
        while(true)
        {
            cout << "请输入数字:";
            cin >> g;
            cnt++;
            if(g>ans) cout << "大了" << endl;
            else if(g<ans) cout << "小了" << endl;
            else
            {
                cout << "猜对了!共" << cnt << "次" << endl;
                break;
            }
        }
        char c;
        cout << "再来一局?";
        cin >> c;
        if(c=='n'||c=='N') break;
    }
    return 0;
}
原文