如何使用second while语句进行连续循环

时间:2014-03-08 03:10:16

标签: c++

好的,这是我的游戏代码叫做猪。我遇到麻烦,因为计算机转动的代码只能通过一次,一旦到达第二个转弯,第一行只会打印出来。有什么解决方法还是我搞砸了?

`int main () {

  int humanTurn=0;
  int humanTotalScore=0;
  int computerTotalScore=0;
  int computerTurn=3;
  float diceRoll = 0;
  int score = 0;
  int computerScore = 0;
  int answer = 0;
  int humanScore = 0;
  int pause = 0;
  float computerDiceRoll = 0;


    srand(time(0));



    cout << "Welcome to the game of pig, the first player to reach to 100 wins."<< endl;
    cout << "if you roll a one no score will be recorded and your turn will go the computer"<< endl;
    cout << " If you choose to hold, the total score accumilated will be recorded" << endl;






  while ((humanTotalScore < 100) && (computerTotalScore < 100)) {
     cout << endl;
     cout << endl;
     cout << "press 1 to  roll or 2 to save your score: ";
     cin >> answer;


  for (int i=0; i<1; i++) {
    diceRoll = rand() % 6 + 1;

  }



 if ((diceRoll > 1) && ( answer == 1)){
    cout << endl;
    cout << endl;
    cout << "your  current roll is: " << diceRoll << endl; 
    cout << endl;
    cout << endl;


      score += diceRoll;
     cout << "Score for this turn is: " << score << endl;
     cout << "your total score is: " << humanTotalScore << endl;
    cout << endl;
    cout << endl;
    }
 else if (answer == 2){
   cout << "your score of: " << score << " will be saved" << endl;
   humanTotalScore += score;
   score = 0;


} 

 else {
  cout << endl;
  cout << endl;
  cout << "you  rolled a ONE. You lose your turn and any points that were with it" <<endl;
  cout << endl;
  cout << endl;

  score = 0;
  computerTurn = score;
  }
  if ((computerTurn == 0) || (answer == 2)){




    computerDiceRoll = rand() % 6 + 1;

这是给我循环问题的代码。

    while ((computerDiceRoll > 1)&& (computerScore <= 20)){

    if (computerDiceRoll == 1){
     computerScore = 0;
     computerTurn = 1;
     cout << "Computer rolled a ONE, it is now your turn." << endl;
 }

      computerDiceRoll = rand() % 6 + 1;
      cout << endl;
      cout << endl;
      cout << "computers roll is : " << computerDiceRoll << endl; 
      cout << endl;
      cout << endl;

      computerScore += computerDiceRoll;
      if (computerScore >20){
        computerTotalScore += computerScore;
        computerTurn = 1;
      }

      cout << "computer current score is : " << computerScore << endl;
      cout << "computer total score is: " << computerTotalScore << endl;
      cout << endl;
      cout << endl;

     }
    }

  }
 } ` 

0 个答案:

没有答案