for循环代码运行正常,但它没有做我想做的事

时间:2015-11-06 15:03:57

标签: loops logic

我的目标是使用使用Die类的PairOfDice类将一对骰子滚动1000次。滚动1000次,每次PaidOfDice总和等于7时,我必须计算它,直到循环终止于1000.所以我做了一个循环计数为1000卷1000。我看一下输出并注意到,当roller.roll()不等于7时,计数有时会增加1.这是我的代码。

import java.util.Scanner;

public class ChidoriDiceRoller 
{
   private PairOfDice roller;
   private int count, sum;
   public static void main(String[] args)
   {
      PairOfDice roller = new PairOfDice();
      int count = 0;

      for(int i = 1; i <= 1000; i++)
      {
         roller.roll();
         System.out.println(i +"/"+ roller.roll() +"/"+ count);

            if (roller.roll() == 7)
            {
               count++;
               //System.out.println(roller.roll() +"/"+count );
            }

      }
   }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////

//********************************************************************
//  PairOfDice.java       Author: Lewis/Loftus
//
//  Solution to Programming Project 4.9 and 5.11
//********************************************************************

public class PairOfDice
{
   private Die die1, die2;

   //-----------------------------------------------------------------
   //  Creates two six-sided Die objects, both with an initial
   //  face value of one.
   //-----------------------------------------------------------------
   public PairOfDice()
   {
      die1 = new Die();
      die2 = new Die();
   }

   //-----------------------------------------------------------------
   //  Rolls both dice and returns the combined result.
   //-----------------------------------------------------------------
   public int roll()
   {
      return die1.roll() + die2.roll();
   }

   //-----------------------------------------------------------------
   //  Returns the current combined dice total.
   //-----------------------------------------------------------------
   public int getTotalFaceValue()
   {
      return die1.getFaceValue() + die2.getFaceValue();
   }

   //-----------------------------------------------------------------
   //  Returns the current value of the first die.
   //-----------------------------------------------------------------
   public int getDie1FaceValue()
   {
      return die1.getFaceValue();
   }

   //-----------------------------------------------------------------
   //  Returns the current value of the second die.
   //-----------------------------------------------------------------
   public int getDie2FaceValue()
   {
      return die2.getFaceValue();
   }

   //-----------------------------------------------------------------
   //  Returns the string representation of this pair of dice.
   //-----------------------------------------------------------------
   public String toString()
   {
      return "Die 1: " + die1.getFaceValue() + "   Die 2: " +
             die2.getFaceValue();
   }
}

/////////////////////////////////////////////// //////////////////////////////// ////////////////////////////////////////////////// /////////////////////////////

//********************************************************************
//  Die.java       Author: Lewis/Loftus
//
//  Solution to Programming Project 5.11
//
//  Represents one die (singular of dice) with faces showing values
//  between 1 and the number of faces on the die.
//********************************************************************

public class Die
{
   private final int MAX = 6;  // maximum face value

   private int faceValue;  // current value showing on the die

   //-----------------------------------------------------------------
   //  Constructor: sets the initial face value.
   //-----------------------------------------------------------------
   public Die()
   {
      faceValue = 1;
   }

   //-----------------------------------------------------------------
   //  Rolls the die and returns the result.
   //-----------------------------------------------------------------
   public int roll()
   {
      faceValue = (int) (Math.random() * MAX) + 1;

      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Face value mutator.
   //-----------------------------------------------------------------
   public void setFaceValue(int value)
   {
      faceValue = value;
   }

   //-----------------------------------------------------------------
   //  Face value accessor.
   //-----------------------------------------------------------------
   public int getFaceValue()
   {
      return faceValue;
   }

   //-----------------------------------------------------------------
   //  Returns a string representation of this die.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = Integer.toString(faceValue);

      return result;
   }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////// 我切出了前900卷,但这里是我的意思的一个例子。它不算数。对于i =卷数

,这是“i / roller.roll()/ count”
900/10/142
901/3/142
902/10/143
903/9/143
904/9/143
905/10/143
906/4/143
907/9/144
908/6/144
909/11/144
910/10/144
911/6/144
912/4/144
913/8/144
914/7/145
915/7/145
916/6/145
917/6/145
918/9/145
919/7/145
920/10/145
921/8/145
922/12/145
923/6/145
924/5/145
925/7/145
926/5/146
927/7/146
928/6/146
929/11/146
930/5/146
931/10/146
932/12/146
933/6/146
934/4/146
935/6/146
936/5/146
937/7/146
938/2/146
939/8/146
940/2/146
941/3/146
942/8/146
943/11/147
944/9/147
945/5/147
946/11/147
947/9/147
948/5/147
949/9/147
950/9/147
951/5/147
952/6/147
953/6/147
954/7/147
955/6/147
956/8/147
957/4/147
958/9/147
959/8/147
960/7/147
961/6/147
962/8/147
963/3/147
964/11/147
965/4/147
966/6/147
967/7/147
968/10/147
969/4/147
970/10/147
971/6/147
972/7/147
973/10/147
974/6/147
975/10/147
976/8/147
977/9/148
978/8/148
979/9/148
980/3/148
981/11/148
982/10/148
983/10/148
984/11/148
985/7/148
986/6/148
987/6/148
988/10/148
989/7/148
990/10/148
991/9/148
992/4/148
993/4/148
994/9/148
995/7/148
996/11/148
997/9/149
998/8/149
999/5/149
1000/11/149

1 个答案:

答案 0 :(得分:0)

每次调用roller.roll()时,它都会再次掷骰子。

您应该将结果存储在变量中,例如

{{1}}

避免滚动次数超过预期。