nanadream 发表于 2015-4-10 21:30:05

Do while class 问题

我的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;
}

nanadream 发表于 2015-4-11 04:23:28

为什么这么多人看,没有人回呀?:funk::sweat::dizzy:不难吧?谁给我讲讲呀:loveliness::loveliness::loveliness::loveliness:小女子不胜感激呀!!!

haiouda 发表于 2015-4-11 21:12:45

你这是C++ ,还是C呀;
我刚开始学,只会C ;
页: [1]
查看完整版本: Do while class 问题