繁体   English   中英

Java死,数着蛇眼

[英]Java Die, Counting Snake Eyes

好的,对于大学来说,我们必须创建两个Die对象并将其滚动几次,计算出现的蛇眼的数量。

这是我的代码,每次编译和执行程序时,我都需要帮助。 我不是100%知道我要去哪里错了,除非您上了大学,否则我不完全知道解决方案是否会有所帮助,但是您会得到任何帮助:)谢谢。

失败是-http://i.imgur.com/ghcOlpP.png

(这全部用JPLIDE编码)

final int ROLLS = 500;
  int num1, num2, count = 0;

  Die die1 = new Die();
  Die die2 = new Die();

  for (int roll=1; roll <= ROLLS; roll++)
  {num1 = 1;
    num2 = 1;

    if (num1 == 1 && num2 == 1)    // check for snake eyes
      count++;
  }

  System.out.println ("Number of rolls: " + ROLLS);
  System.out.println ("Number of snake eyes: " + count);
  System.out.println ("Ratio: " + (float)count / ROLLS);
}}}

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 of this die.
  //-----------------------------------------------------------------
  public Die()
  {faceValue = 1;
  }

  //-----------------------------------------------------------------
  //  Computes a new face value for this die and returns the result.
  //-----------------------------------------------------------------
  public int roll()
  {faceValue = (int)(Math.random() * MAX) + 1;
    return faceValue;
  }

  //-----------------------------------------------------------------
  //  Face value mutator. The face value is not modified if the
  //  specified value is not valid.
  //-----------------------------------------------------------------
  public void setFaceValue (int value)
  {if (value > 0 && value <= MAX)
    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;
  }
}
for (int roll=1; roll <= ROLLS; roll++) {
num1 = 1;
num2 = 1;

if (num1 == 1 && num2 == 1)    // check for snake eyes
  count++;
}

这看起来不对。 您设置num1 = 1num2 = 1 ,然后检查它们是否都等于1。当然,它们将等于1。我假设您要执行类似的操作

num1 = die1.roll() num2 = die2.roll()

至少可以做到,这样您就不会有500次蛇眼。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM