java - 骰子模拟java

标签 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.");
}

}

最佳答案

这是你的问题

if(die1Value == die2Value)
    count++;

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

例如,如果掷出 4 和 4,则 count 将递增,并且以下 else if 永远不会运行:

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

关于java - 骰子模拟java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44427075/

相关文章:

java - IntelliJ 中字符串内的大括号匹配

java - 使用 Jackson 将 Java 列表序列化为 XML 和 JSON

java - 使用 ant 构建项目时包含 .properties 文件

java - 扫描仪在使用 next() 或 nextFoo() 后跳过 nextLine()?

java - 反序列化 XML 文件而不转换 xml 文件

java - 更高质量的 JFreeChart 图表

Java Web应用程序 "Event Listener for Expired Sessions"

java - 使用 mysql 分割 .csv 文件

java - Spring Boot/JUnit,为多个配置文件运行所有单元测试

java - 无法将 xml 转换为 jaxb