You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++循环实现决策逻辑:数字猜谜游戏代码优化求助

C++数字猜谜游戏代码优化方案

1. 冗余逻辑精简

  • 原代码中判断猜错的条件if (num1 < num2 || num1 > num2)等价于if (num1 != num2),直接替换即可,逻辑更清晰
  • 末尾两处cout << "Game Over"是重复代码,可以统一放到最后输出,不用分分支写
  • 修正拼写错误:Your correct改为You're correct
  • 原代码中system("cls")在猜对、猜错分支各写了一次,可以直接抽到分支判断外,减少重复代码
  • 剩余次数提示补充tries描述,原输出You have X left.改为You have X tries left.,语义更明确

2. 循环逻辑优化

原do-while循环里用break跳出的逻辑可以简化,将猜测结果作为循环终止条件的一部分,减少break的使用,用for循环替代do-while,剩余次数的变量生命周期直接限定在循环内,代码更紧凑:

bool is_win = false;
for (int remaining = 3; remaining > 0 && !is_win; remaining--) {
    // 猜测逻辑
    if (guess == target_num) is_win = true;
}

3. 用户体验优化

猜错时可以直接提示用户猜大了还是猜小了,不用只提示错误,玩家可以快速缩小猜测范围:

if (guess > target_num) {
    cout << "Too big! Try again." << endl;
} else {
    cout << "Too small! Try again." << endl;
}

4. 鲁棒性增强

现有代码如果用户输入非数字内容,cin会进入错误状态导致程序死循环,增加输入合法性校验:

while (!(cin >> num) || num <=0 || num >99) {
    cout << "Invalid input! Please enter a number between 1 and 99: ";
    cin.clear(); // 清空错误标志
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // 清空缓冲区垃圾内容
}

注意需要引入<limits>头文件使用numeric_limits。
另外system("cls")、system("pause")是Windows平台专属接口,如果需要跨平台运行可以替换为对应平台的清屏、暂停逻辑,作业场景下如果仅在Windows运行可以保留。


优化后完整代码

#include <iostream>
#include <limits>
using namespace std;

int main()
{
    int target_num = 0, guess = 0;
    bool is_win = false;

    cout << "***Number Guessing Game***" << endl;
    cout << "Player 1 will pick a 1 to 2 digit number." << endl;
    cout << "Player 2 will have 3 chances to guess correctly." << endl << endl;

    // 玩家1输入合法数字
    while (!(cin >> target_num) || target_num <= 0 || target_num > 99)
    {
        cout << "Invalid input! Player 1 please pick a number (1 to 99): ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    system("cls");

    // 玩家2猜数字
    for (int remaining_tries = 3; remaining_tries > 0 && !is_win; remaining_tries--)
    {
        cout << "You have " << remaining_tries << " tries left." << endl;
        cout << "Player 2, pick a number (1 to 99): ";
        while (!(cin >> guess) || guess <= 0 || guess > 99)
        {
            cout << "Invalid input! Please enter a number between 1 and 99: ";
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
        }

        system("cls");
        if (guess == target_num)
        {
            cout << "You're correct, you win!" << endl;
            is_win = true;
        }
        else if (guess > target_num)
        {
            cout << "Too big! Try again." << endl;
        }
        else
        {
            cout << "Too small! Try again." << endl;
        }
    }

    if (!is_win)
    {
        cout << "Too many attempts! Try again next time." << endl;
    }
    cout << "Game Over" << endl;
    
    system("pause>0");
    return 0;
}

内容的提问来源于stack exchange,提问作者Charles Spangler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 20:30:01