从卷筒打印负数结果

negative number result printing from roll

本文关键字:结果 打印      更新时间:2023-10-16

我知道标题有点模糊,但我正在构建一个掷骰子的程序(正如你们在课程中永远看到的那样)让我困惑的是我的输出结果。

我只滚动一个六面骰子,并计算每一张脸的出现次数。我的结果以列表形式打印id,但是如果当我告诉程序滚动30次时,我收到的结果是-858993460。我不知道它可能来自哪里,我希望有人能告诉我哪里出了问题,我会发布我的标题和源文件代码,其中一些在我留下评论的某些领域是不完整的。

//Dice.h
#pragma once
#include <iostream>

使用命名空间std;

class aDie {
public:
    aDie();
    int Roll();
    int getRoll();
    void setRolls(int r);
    int getTotal();
    void setTotal(int t);
    int numOnes = 0, numTwos = 0, numThrees = 0, numFours = 0, numFives = 0, numSixes = 0;
    int rollTotal();
    int Display();
    int numRolls;
    int allRolls = 0;
    int randomRoll;
protected:
    int roll;
    int totalRoll;
    int DisplaySomeRolls;
};
aDie::aDie() {
}
int aDie::getRoll() {
    return roll;
}
void aDie::setRolls(int r) {
    roll = r;
}
int  aDie::getTotal() {
    return  totalRoll;
}
void aDie::setTotal(int t) {
    totalRoll = t;
}
int aDie::Roll() {
    //roll the dice here
    return 0;
}
int aDie::Display() {
    int DiceFaces[6]  { numOnes, numTwos, numThrees, numFours, numFives, numSixes };
return DisplaySomeRolls;
    //use this to display the outputs of each face
}
// ProjectNumberTwo.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "Dice.h"
using namespace std;
int main(){
    aDie getRolls;
    aDie DiceFaces[6];
    int  numRolls    = 0;
    int  rollTotal   = 0;
    int  numOnes     = 0, numTwos = 0, numThrees = 0, numFours = 0, numFives = 0, numSixes = 0;
    int  randomRoll  = rand() % 6 + 1;

    srand((unsigned int)time(NULL));
    cout << "How many rolls? "; // GEt user input of how many rolls
    cin >> numRolls;
    cin.ignore();

    cout << "**Dice Roll Statistics***" << endl;
    getRolls.setRolls(numRolls);

    if (numRolls >= 1) {  // Rolls dice numRolls times
        for (int i = 0; i < 6; i++) //makes a for-loop that makes an array of all the rolls from the user input
            DiceFaces[i].setRolls(randomRoll);
        numRolls = randomRoll;
        if (rollTotal == 1) { // Count number of occurences 1-6
            numOnes += 1;
        }
        else if (rollTotal == 2) {
            numTwos += 1;
        }
        else if (rollTotal == 3) {
            numThrees += 1;
        }
        else if (rollTotal == 4) {
            numFours += 1;
        }
        else if (rollTotal == 5) {
            numFives += 1;
        }
        else if (rollTotal == 6) {
            numSixes += 1;
        }
    }
    for (int i = 1; i < 6; i++)
        cout << i << ": " << DiceFaces[i].Display() << endl;

    // The printout needs to be rollFaces so it prints how many times the face  
    // happened, not getroll which only shows one single roll
    system("pause");
    return 0;
}

为什么要这样做:

numRolls = randomRoll;

此外,请查看您将模具辊设置为什么以及何时进行随机辊分配。

老实说,你使用的逻辑似乎比它需要的要复杂得多。你的代码太天真了,我建议你从头开始(无意冒犯)。写一些代码,将10个骰子分配给你的计数器,然后从那里扩展,(不要害怕作弊者)。

我希望这对一些人有所帮助。

试试这个,让我知道它是否适合你!

#include<iostream>
#include<stdlib.h>
using namespace std;
class dice
{
public:
    int a,z=0,y=0,x=0,w=0,v=0,u=0;
    void number_generation()
    {
        a=rand()%6+1;
    }
    void checkcount(int t)
    {
        if(t==1)z++;
        else if(t==2)y++;
        else if(t==3)x++;
        else if(t==4)w++;
        else if(t==5)v++;
        else u++;
    }
    void display()
    {
        cout<<"OCCURENCE OF EACH NUMBER IS AS FOLLOWS:n";
        cout<<"1-"<<z<<endl<<"2-"<<y<<endl<<"3-"<<x<<endl<<"4-"<<w<<endl<<"5-"<<v<<endl<<"6-"<<u<<endl;
    }
};
int main()
{
    int i,throws;
    dice d;
    cout<<"Enter the number of throws";
    cin>>throws;
    for(i=0;i<throws;i++)
    {
        d.number_generation();
        d.checkcount(d.a);
    }
    d.display();
}