防止两个随机整数的减法等于负整数?

Preventing the subtraction of two random integers from equaling a negative integer?

提问人:firstpostevermade 提问时间:11/20/2022 最后编辑:Rohan Barifirstpostevermade 更新时间:11/20/2022 访问量:58

问:

简单的数学游戏.cpp。

case(2) 是两个随机整数的减法,但 ** 结果不能小于 0。 ** 我需要“subOp”来保存 'ranNum1' - 'ranNum2' 的值,当它们等于 >= 0 时

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

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

    // Declares variable as a random integer between 0-12
  //  int ranNum1 = rand() % 13;
  //  int ranNum2 = rand() % 13;
    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)
    {
        int ranNum1 = rand() % 13;
        int ranNum2 = rand() % 13;
        switch (menuChoice)
        {
        case (0):
            break;

        case (1): { 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 subOp = ranNum1 - ranNum2;
            do {
                cout << "\tWhat is " << ranNum1 << " - " << ranNum2 << " = [?]\n\tYour answer: ";
                cin >> userAns;
                if (userAns == subOp) {
                    cout << cAns;
                }
                else {
                    cout << wAns << "\n\tThe correct answer was " << subOp;
                }
    
            } while (subOp < 0);
            break; }

        case (3):{ 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;
}

我尝试了几种不同的循环和布尔值。如何循环方程式,直到结果仅等于 >0?

测试 布尔逻辑 C++ 循环

评论

1赞 PaulMcKenzie 11/20/2022
与其把所有东西都塞进去,不如创建单独的函数来执行你试图模拟的任何操作。main
1赞 drescherjm 11/20/2022
怎么样:if (ranNum1 < ranNum2) std::swap(ranNum1,ranNum2);
1赞 Peter 11/20/2022
始终从较大的值中减去较小的值。检查哪个更小,并根据需要采取不同的操作是微不足道的。

答:

1赞 Rohan Bari 11/20/2022 #1

您可以创建一个条件语句来检查第二个值是否大于第一个值。如果是这样,请在它们之间交换。

if (ranNum1 < ranNum2)
    std::swap(ranNum1, ranNum2);

int subOp = ranNum1 - ranNum2;

现在,的值总是大于 。ranNum1ranNum2