C ++骰子滚动和图形

时间:2018-10-26 01:54:39

标签: c++ graph dice

#include <iostream>
#include<iomanip>
#include <array>
#include<string>
using namespace std;

int main()
{
    const int arraysize = 13;
    string n;
    int counter[13];
    double sum=0;
    // init counter
    for (int i = 0; i < 13; i++)
        counter[i] = 0;

    int die1;
    int die2;

    for (int roll1 = 0; roll1 <= 36000000; roll1++) {
        die1 = 1 + rand() % 6;
        die2 = 1 + rand() % 6;
        counter[die1 + die2]++;
    }

    cout << "Rolls" << setw(13) << "Frequency" <<  endl;

    for (int face = 2; face < arraysize; face++)
    {
        for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {
            cout <<  '*'; //output for graph
        }
        sum = ((sum + counter[face]) / 36000000) * 100; 
        cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum << endl;    
    }

    system("Pause");
    return 0;
}

代码输出

enter image description here

我试图获得这样的输出,但是我的图形并没有以我想要的方式显示出来。任何建议我该怎么做?如何正确放置它。

enter image description here

这是我的代码输出。

1 个答案:

答案 0 :(得分:-1)

所以我最终弄清楚了。我最终将图形放置在代码的不同部分,并使其正常工作。

for (int face = 2; face < arraysize; face++)
{

    sum = ((sum + counter[face]) / 36000000) * 100;
    cout << setw(7) << face << setw(13) << counter[face] << setw(15) << fixed << setprecision(1) << sum;


    for (int s = (counter[face] * 100 / 36000000); s > 0; s--) {

        cout << '*';

    }
    cout << endl;
}
system("Pause");
return 0;
}

谢谢你的建议!