|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我的code是随机掷色子,记录下每次色子的值并加和,当综合大于某个数时比如20停止。code 如下。我现在只会用 for loop, 比如loop 10次。请问各位大侠,我想用do {} while();写, do{mydice.roll(); mydice.display();} while (totalscore<20);
怎么不行啊,那个totalscore的值应该如何读取呢?非常感谢!还有,这个问题如果想用指针应该怎么写呢?
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class dice{
public:
dice();
void roll();
void display();
void reset();
private:
int lastroll;
int totalroll;
int totalscore;
};
dice:: dice() : lastroll(0), totalroll(0), totalscore(0)
{}
void dice::roll(){
lastroll = 0;
totalroll++;
lastroll = (rand() % 6) + 1;
totalscore += lastroll;
}
void dice::display(){
cout << "your last roll was" << lastroll << endl;
cout << "you have rolled " << totalroll << " times." << endl;
cout << "your total score is " << totalscore << endl;
}
void dice::reset(){
lastroll = 0;
totalscore = 0;
totalroll = 0;
}
int main(){
srand(static_cast<unsigned int>(time(0)));
dice mydice;
mydice.display();
for (int i = 0; i < 10; i++){
mydice.roll();
mydice.display();
}
mydice.reset();
mydice.display();
cin.get();
return 0;
}
|
|