C++ 如何将for循环创建的可变数量的整数相加?

C++ 如何将for循环创建的可变数量的整数相加?,c++,C++,好吧,我不确定上面的问题是否真的说明了我的问题,但我花了几个小时寻找答案。基本上,我正在创建一个掷骰子程序,它将要求用户输入掷骰子的数量、骰子的边数,然后显示结果。但是,我也希望能够添加所有卷的总数。不过,我不知道该怎么做。 以下是我所拥有的: #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int main() { int roll, side,

好吧,我不确定上面的问题是否真的说明了我的问题,但我花了几个小时寻找答案。基本上,我正在创建一个掷骰子程序,它将要求用户输入掷骰子的数量、骰子的边数,然后显示结果。但是,我也希望能够添加所有卷的总数。不过,我不知道该怎么做。 以下是我所拥有的:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
    int roll, side, dice, i;
    srand(time(0));
    cout << "-------------------" << endl;
    cout << "      D I C E      " << endl;
    cout << "    R O L L E R    " << endl;
    cout << "*******v1.00*******" << endl;
    cout << endl;
    do
    {
        cout << "Enter the number of dice you wish to roll.   " << endl;
        cin >> dice;
        cout << endl;
        cout << "Enter the number of sides on the dice.       " << endl;
        cin >> side;
        cout << endl;
            for (i = 0; i < dice; ++i) //Increments for the number of dice rolled.
            {
                roll = rand() % side + 1; //Allows for variable sides based on input.
                cout << endl;
                cout << "[" << roll << "]" << endl; //Rolls are displayed in a
                cout << endl;                       //column in brackets.
            }
    } while (true);
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数卷,边,骰子,i;
srand(时间(0));

cout将sum变量定义为0,并继续使用roll添加它,并在屏幕上显示它,如下所示

int rollSum = 0;
cout << endl;
do
{
    cout << "Enter the number of dice you wish to roll.   " << endl;
    cin >> dice;
    cout << endl;
    cout << "Enter the number of sides on the dice.       " << endl;
    cin >> side;
    cout << endl;
        for (i = 0; i < dice; ++i) //Increments for the number of dice rolled.
        {
            roll = rand() % side + 1; //Allows for variable sides based on input.
            rollSum += roll;
            cout << endl;
            cout << "[" << roll << "]" << rollSum << endl; //Rolls are displayed in a
            cout << endl;                       //column in brackets.
        }
} while (true);
int rollSum=0;

cout只需引入一个累加所有转鼓的变量:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
    int roll, side, dice, i;
    srand(time(0));
    cout << "-------------------" << endl;
    cout << "      D I C E      " << endl;
    cout << "    R O L L E R    " << endl;
    cout << "*******v1.00*******" << endl;
    cout << endl;
    do
    {
        cout << "Enter the number of dice you wish to roll.   " << endl;
        cin >> dice;
        cout << endl;
        cout << "Enter the number of sides on the dice.       " << endl;
        cin >> side;
        cout << endl;
        int total = 0;                          // <<<<<<<<<
        for (i = 0; i < dice; ++i) //Increments for the number of dice rolled.
        {
            roll = rand() % side + 1; //Allows for variable sides based on input.
            total += roll;                      // <<<<<<<<<
            cout << endl;
            cout << "[" << roll << "]" << endl; //Rolls are displayed in a
            cout << endl;                       //column in brackets.
        }
        cout << "total =" << total << endl;     // <<<<<<<<<
    } while (true);
    return 0;
}
#包括
#包括
#包括
使用名称空间std;
int main()
{
整数卷,边,骰子,i;
srand(时间(0));

cout
int sum=0;
然后
sum+=roll;
在循环内。这就是你想要的吗?啊,好的,所以+=将继续添加,因为for循环运行了很多次。我想我现在明白了。它确实有效,谢谢你。当然-你可以在主循环内或外执行此操作,具体取决于你需要什么,但是你得到了一般的结果我不知道。