从卷筒打印负数结果
negative number result printing from roll
我知道标题有点模糊,但我正在构建一个掷骰子的程序(正如你们在课程中永远看到的那样)让我困惑的是我的输出结果。
我只滚动一个六面骰子,并计算每一张脸的出现次数。我的结果以列表形式打印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();
}
相关文章:
- C++使用 g++ 时,没有结果,没有打印
- oStream 不打印添加两个 valarray 的结果(使用运算符重载)
- cout 打印不准确的结果,printf 打印准确的结果
- 读取一组用户输入,按升序排序,然后打印结果
- 无法打印正确的结果
- 为什么C++打印出奇怪的结果的最小值?
- 对象指针打印结果以相反的顺序进行
- 硬编码字符串与强制转换为 PUCHAR 并打印到控制台时从控制台读取的字符串的结果不同
- 尝试以随机字符(长读取)打印字符串返回结果
- GetTempPathA 函数在打印结果后崩溃
- 我定义的反向函数不是打印结果
- 声明双重给它一个值0,但打印结果意外变化
- 打印整数指针元素时的结果不同
- 如何打印返回值的结果?我是一个初学者学习C++
- COUT没有打印出预期的结果
- 如何在VS 2017 C++Win32应用程序中打印出gethostname结果
- 我的程序打印出与预期不同的结果
- 尝试将二进制数组转换为十六进制C++然后打印结果
- 在WINDBG中调用C 方法实例并打印结果
- 井字游戏-如何没有无效输入后的结果打印