骰子模拟java

时间:2017-06-08 05:09:06

标签: java

我正在编写一个骰子模拟程序,但是我遇到了结果问题,因为它只是打印出来:

You rolled a ...1 and 1

You rolled double ones 0 out of 10000 rolls.
You rolled double twos 0 out of 10000 rolls.
You rolled double threes 0 out of 10000 rolls.
You rolled double fours 0 out of 10000 rolls.
You rolled double fives 0 out of 10000 rolls.
You rolled double sixes 0 out of 10000 rolls.

它总是打印这个,我不明白为什么,因为它应该每次打印不同的数字。

    public class Dice
{
    private int die1;
    private int die2;

    public Dice()
    {
        die1 = 1;
        die2 = 1;
    }

    public void rollDice()
    {
        Random myRan = new Random();

        die1 = myRan.nextInt(6) + 1;
        die2 = myRan.nextInt(6) + 1;
    }

   public int getDie1()
   {
   return die1;
   }


   public int getDie2()
   {
    return die2;   
   }


    public String toString()
    {
         return "You rolled a ..." + die1 + " and " + die2 + "\n";   }
}

和...

 import java.util.Random;
public class DiceSimulation
{
    static final int NUMBER = 10000; 
    static Random generator = new Random();

    static int die1Value; 
    static int die2Value; 
    static int count = 0; 
    static int ones = 0; 
    static int twos = 0; 
    static int threes = 0; 
    static int fours = 0; 
    static int fives = 0; 
    static int sixes = 0; 
    static int c=1;

public static void main(String[] args)
{

    rollDiceAndTabulateWhile(); 
    rollDiceAndTabulteDoWhile();   
    rollDiceAndTabulteFor();   
    summarizeResults();
}

public static void rollDiceAndTabulateWhile()
{

        Dice d = new Dice();
        while(c<= NUMBER)
        {
        d.rollDice();
        die1Value = d.getDie1();
        die2Value = d.getDie2();
        if(die1Value == die2Value)
        count++;
        else if(die1Value == 1 && die2Value == 1)
        ones++;
        else if(die1Value == 2 && die2Value == 2)
        twos++;
        else if(die1Value == 3 && die2Value == 3)
        threes++;
        else if(die1Value == 4 && die2Value == 4)
        fours++;
        else if(die1Value == 5 && die2Value == 5)
        fives++;
        else if(die1Value == 6 && die2Value == 6)
        sixes++;

        c++;
        }
}

public static void rollDiceAndTabulteDoWhile()
{

     DiceSimulation dw = new DiceSimulation();

        Dice d = new Dice();
        do
        {
        d.rollDice();
        dw.die1Value = d.getDie1();
        dw.die2Value = d.getDie2();
        if(die1Value == die2Value)
        count++;
        else if(dw.die1Value == 1 && dw.die2Value == 1)
        ones++;
        else if(dw.die1Value == 2 && dw.die2Value == 2)
        twos++;
        else if(dw.die1Value == 3 && dw.die2Value == 3)
        threes++;
        else if(dw.die1Value == 4 && dw.die2Value == 4)
        fours++;
        else if(dw.die1Value == 5 && dw.die2Value == 5)
        fives++;
        else if(dw.die1Value == 6 && dw.die2Value == 6)
        sixes++;
        c++;
        } while(c <= NUMBER);
}  

public static void rollDiceAndTabulteFor()
  {
    DiceSimulation dw = new DiceSimulation();
    Dice d = new Dice();

    for(c=1;c<=NUMBER;c++)
    {
    d.rollDice();
    dw.die1Value = d.getDie1();
    dw.die2Value = d.getDie2();
    if(die1Value == die2Value)
    count++;
    else if(dw.die1Value == 1 && dw.die2Value == 1)
    ones++;
    else if(dw.die1Value == 2 && dw.die2Value == 2)
    twos++;
    else if(dw.die1Value == 3 && dw.die2Value == 3)
    threes++;
    else if(dw.die1Value == 4 && dw.die2Value == 4)
    fours++;
    else if(dw.die1Value == 5 && dw.die2Value == 5)
    fives++;
    else if(dw.die1Value == 6 && dw.die2Value == 6)
    sixes++;
  }
  }
public static void summarizeResults()
{
    Dice d = new Dice();
    System.out.println(d.toString());
    System.out.println("You rolled double ones " + ones
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double twos " + twos
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double threes " + threes
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double fours " + fours
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double fives " + fives
            + " out of " + NUMBER + " rolls.");
    System.out.println("You rolled double sixes " + sixes
            + " out of " + NUMBER + " rolls.");
}

}

2 个答案:

答案 0 :(得分:1)

这是你的问题

if(die1Value == die2Value)
    count++;

当此陈述为真时,您的else if语句都不会运行。

例如,如果您滚动4和4,则count将递增,而以下其他if将永远不会运行:

else if(die1Value == 4 && die2Value == 4)
        fours++; // Never runs

答案 1 :(得分:0)

当您每次调用Random时,您正在创建rollDice()对象。它不好,使它成为实例变量。

这不是代码中的问题。问题在于您的ifelse if条件,因为如果一个条件true未达到其他else ifelse,那么{ {1}},ifelse if。因为,您需要执行所需的所有增量,将它们更改为单独的else条件。

if更改为else if

if

还要确保缩进代码。

输出:

    if(die1Value == die2Value)
      count++;
    if(die1Value == 1 && die2Value == 1)
      ones++;
    if(die1Value == 2 && die2Value == 2)
      twos++;
    if(die1Value == 3 && die2Value == 3)
      threes++;
    if(die1Value == 4 && die2Value == 4)
      fours++;
    if(die1Value == 5 && die2Value == 5)
      fives++;
    if(die1Value == 6 && die2Value == 6)
      sixes++;

<强>更新

获得

You rolled double ones 286 out of 10000 rolls.
You rolled double twos 280 out of 10000 rolls.
You rolled double threes 265 out of 10000 rolls.
You rolled double fours 262 out of 10000 rolls.
You rolled double fives 278 out of 10000 rolls.
You rolled double sixes 303 out of 10000 rolls.

变化:

You rolled a ...4 and 3