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

如何确保C++数学游戏中随机整数相减结果非负?

解决减法运算结果非负的问题

你的核心问题是当前代码的减法逻辑搞反了循环时机——你应该先生成满足ranNum1 >= ranNum2的随机数对,再输出题目,而不是生成完数字后才循环提问(这样只会重复问同一个结果为负的题目)。

具体修改方案

在case(2)中,先添加循环生成合法的随机数对,确保ranNum1 >= ranNum2,之后再计算subOp并进行提问:

case (2): {
    // 循环生成随机数,直到ranNum1 >= ranNum2
    int ranNum1, ranNum2;
    do {
        ranNum1 = rand() % 13;
        ranNum2 = rand() % 13;
    } while (ranNum1 < ranNum2);
    
    int subOp = ranNum1 - ranNum2;
    cout << "\tWhat is " << ranNum1 << " - " << ranNum2 << " = [?]\n\tYour answer: ";
    cin >> userAns;
    if (userAns == subOp) {
        cout << cAns;
    } else {
        cout << wAns << "\n\tThe correct answer was " << subOp;
    }
    break;
}

为什么原来的代码无效?

你之前的do-while循环是在提问后判断subOp < 0,但此时ranNum1和ranNum2已经固定,subOp的值不会改变,循环只会反复让用户回答同一个结果为负的题目,完全达不到重新生成合法数字的目的。

修改后的完整代码

#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

int main()
{
    // Initialize random number generator.
    srand(time(0));

    int menuChoice = 0;
    int userAns=0;
    
    // String variable for the game menu
    string mathMenu =
        "\n\n\tMATH GAME SELECTION\n\t-------------------\n\t1) Addition\n"
        "\t2) Subtraction\n\t3) Multiplication\n\t4) -EXIT-\n";
   // Answer responses
    string cAns = "\n\tCORRECT ANSWER!" , wAns = "\n\tWRONG ANSWER!";

    // While the user doesnt request to exit...
    while (menuChoice != 4)
    {
        switch (menuChoice)
        {
        case (0):
            break;

        case (1): { 
            int ranNum1 = rand() % 13;
            int ranNum2 = rand() % 13;
            int addOp = ranNum1 + ranNum2;
            cout << "\tWhat is " << ranNum1 << " + " << ranNum2 << " = [?]\n \tYour answer: ";
            cin >> userAns;
             if (userAns == addOp) {
                cout << cAns; }
            else{ 
                cout << wAns << "\n\tThe correct answer was " << addOp; }
            break; }

        case (2): { 
            int ranNum1, ranNum2;
            // 生成合法的被减数和减数
            do {
                ranNum1 = rand() % 13;
                ranNum2 = rand() % 13;
            } while (ranNum1 < ranNum2);
            
            int subOp = ranNum1 - ranNum2;
            cout << "\tWhat is " << ranNum1 << " - " << ranNum2 << " = [?]\n\tYour answer: ";
            cin >> userAns;
            if (userAns == subOp) {
                cout << cAns;
            } else {
                cout << wAns << "\n\tThe correct answer was " << subOp;
            }
            break; }

        case (3):{ 
            int ranNum1 = rand() % 13;
            int ranNum2 = rand() % 13;
            int multOp = ranNum1 * ranNum2;
            cout << "\tWhat is " << ranNum1 << " x " << ranNum2 << " = [?]\n \tYour answer: ";
            cin >> userAns;
            if (userAns == multOp) {
                cout << cAns; }
            else {
                cout << wAns << "\n\tThe correct answer was " << multOp; }
            break; }
//Error message/ validator for integers between 1-4.
        default: cout << "\t**Invalid menu selection** \n\a";
        }
//Error message/ validator for input other than integer.
        cout << mathMenu << endl << "   Please Select An Option (1-4): ";
            while (!(cin >> menuChoice)) {
            cin.clear();
            cin.ignore(1000, '\n');
            cout << "Please ONLY select the given options (1-4): \a";
        }
        
        cout << endl;
    }

    system("pause");
    return 0;
}

额外优化点

你原来在循环开头生成的通用ranNum1和ranNum2对减法场景无效,所以把各case里的随机数生成单独实现,避免无效的数字生成逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 06:45:32