好的,我即将完成我的练习,并坚持如何让每个骰子滚动自己的个人随机生成的数字。该程序实际上会滚动随机数,只是每次您重新滚动两个骰子时总是滚动相同的确切数字。发生了这个简单但令人头疼的问题,出于某种原因,我也遇到了
cout << "Adding both dices up you rolled a total of: " << totalScore() << "." << endl;
一位同学还告诉我,我的 faceValue 是非法值,应该设置为合法值。我不太明白他的意思,我相信这会(不是很多)我的一些成绩。
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
class PairOfDice
{
private:
int diceOne;
int diceTwo;
int score;
int faceVaule;
public:
PairOfDice(){
srand(time(NULL));
roll();
}
void roll(){
diceOne = (rand() % 6) + 1;
diceTwo = (rand() % 6) + 1;
setdiceOne(diceOne);
setdiceTwo(diceTwo);
}
void setdiceOne(int value){
faceVaule = value;
}
int getdiceOne(){
return faceVaule;
}
void setdiceTwo(int value){
faceVaule = value;
}
int getdiceTwo(){
return faceVaule;
}
void totalScore(){
score = diceOne + diceTwo;
}
void display(){
cout << "The first Dice rolled a " << getdiceOne() << " ." << endl;
cout << "The second Dice rolled a " << getdiceTwo() << " ." << endl;
// adding both dices gives an: No operator " < < " matches these operands
cout << "Adding both dices up you rolled a total of: " << totalScore() << "." << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
PairOfDice game;
game.roll();
game.display();
game.totalScore();
return 0;
}