gpt4 book ai didi

C++ 我的数组没有保存我的卷

转载 作者:行者123 更新时间:2023-11-28 05:44:50 25 4
gpt4 key购买 nike

<分区>

我不知所措,并尝试将所有内容移入和移出循环。我滚动了 20 次,打印阵列时只出现了一卷。我错过了什么或做错了什么?另外,如何缩短我的 ifelse if 语句,使其在 aDie cpp 中不那么多余?

解决方案是在构造函数中声明我的数组工作代码如下。

头文件aDie.h

#define aDie_H 
class aDie {
public:
aDie();
void numRolls();
void getSeed();
void roll();
void rolls();
void array();
void Print();

private:
int Rolls;
int dSeed;
int die1;
int die2;
int sum;
int DieFace[6];
int i = 0;
};

Cpp文件aDie.cpp

#include "aDie.h" 
#include <iostream>
#include <ctime>
using namespace std;

aDie::aDie() { //constructor sets values to default 0
die1 = 0;
die2 = 0;
dSeed = 0;
DieFace[0] = 0; //declare array
DieFace[1] = 0;
DieFace[2] = 0;
DieFace[3] = 0;
DieFace[4] = 0;
DieFace[5] = 0;
}
void aDie::numRolls() {
cout << "Enter number of times for die to be rolled: ";
cin >> Rolls;
return;
}
void aDie::getSeed() { //gets seed
cout << "Enter desired seed number: ";
cin >> dSeed;
srand(dSeed);
return;
}
void aDie::roll() { //rng for one die
die1 = 1 + rand() % 6;
}
void aDie::array() {
DieFace[die1 - 1] = DieFace[die1 - 1] + 1;
}

void aDie::Print() {
for (i = 0; i < 6; i++) { //loop to display dieFace[i] frequency
cout << i + 1 << " Was Rolled: " << DieFace[i] << " Times." << endl;
}
}

main.cpp

#include <iostream>
using namespace std;

#include "aDie.h"

void game1();

int main(){
game1();

system("Pause");
}

void game1(){
int i = 0;
int Rolls = 0;

cout << "Enter number of times for die to be rolled: ";
cin >> Rolls;

aDie Roll1; //calls default constructor
Roll1.getSeed(); //calls seed

for (i = 0; i < Rolls; i++) {
Roll1.roll(); //rolls die
Roll1.array();
}

Roll1.Print();
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com