我知道标题有点模糊,但是我正在制作一个掷骰子的程序(因为你在课程中永远都看到过),令我困惑的是我的输出结果。
我只是滚动一个六面骰子并计算每张脸的出现次数。我的结果打印出列表形式的ID,但是当我告诉程序滚动说30次时,我收到的结果是-858993460。我不知道它可能来自哪里,我希望有人可以告诉我出错的地方,发布我的标题和源文件代码,其中一些在我留下评论的某些区域是不完整的。< / p>
//Dice.h
#pragma once
#include <iostream>
使用namespace 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;
}
答案 0 :(得分:0)
你为什么这样做:
numRolls = randomRoll;
此外,请查看您设置模具滚动的内容以及进行随机滚动分配的时间。
老实说,你使用的逻辑似乎比它需要的复杂得多。你的代码很天真,我建议你从头开始(没有冒犯)。写一些代码将10个骰子卷分配给你的计数器,然后从那里扩展,(不要害怕破坏者)。
我希望这会有所帮助。
答案 1 :(得分:0)
尝试一下,让我知道它是否适合您!
#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();
}