提问者:小点点

从两个骰子(6面)。我如何在C++中找到给出9之和的数字?(例如die1+die2=9)[闭合]


它循环1-6并分配两个值,num1和NUM2。逻辑operation/if语句有问题,因为某种原因它在第一个地方不起作用。有人帮忙吗?

#include <iostream>

using namespace std;

int main()
{
    for (int i = 1; i <= 6; ++i){
        int num1 = i;
        int num2 = i;
        if (num1 + num2 == 9){
            cout << num1 << " + " << num2 << " = " << " 9 " << endl;
        }
    }



}

共2个答案

匿名用户

我还没试过,但我会试一下:

for (loop in your first dice){
    for (loop in your second dice){
        add the 2 numbers together;
        if (the sum is equal to 9) {
            cout << sum;
        }
    }
}

double for循环应该检查每个数字与第二个骰子的所有数字,并检查它们是否等于9。

编辑伪代码的更改代码

匿名用户

您应该使用双嵌套循环并检查每个骰子的值之和。

#include <iostream>

using namespace std;

int main()
{
    int die[6] = {1, 2, 3, 4, 5, 6};
    int die2[6] = {1, 2, 3, 4, 5, 6};

    int i, x;

    for (i = 0; i < 6; ++i){
        for (x = 0; x < 6; ++x){
            if (die[i] + die2[x] == 9){
                cout << die[i] << ' ' << die2[x] << '\n';
            }
        }
    }


}

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(两个|骰子|面|何在|c++|中找到|给出|之和|数字|die1+die2|闭合)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?