0

例如:如果该数字已在所有骰子掷骰中掷出 25%,则声明 (System.out.println("The dice might be loaded!")

import javax.swing.*;
class Dice
{
    public static void main (String [] args)
    {
            int c = Integer.parseInt(JOptionPane.showInputDialog("How many sides?"));
            int[ ] sides = new int[c];
            String[ ] number = new String[c];
            int max = c;
            int min = 1;
            int d = Integer.parseInt(JOptionPane.showInputDialog("How many times do you want to roll your dice?"));

            for(int i=0;i<c;i++)
            {
                number[i] = (JOptionPane.showInputDialog("Enter side number:"));
            }

            for(int i=0;i<d;i++)
            {
                int r;
                r = (int) Math.floor(Math.random() * max) + min;                System.out.println(r);

                sides[r-1] = sides[r-1] + 1;
            }

            for(int i=0;i<c;i++)
            {
                System.out.println(number[i] + " was rolled " + sides[i] + " times.");
            }
    }
}   
4

2 回答 2

0

没有一个。您需要计算卷数并在代码中执行计算。就像是,

for (int i = 0; i < c; i++) {
  double freq = ((double) sides[i]) / d;
  if (freq > 0.25) {
    // ...
  }
  System.out.printf("%s was rolled %d times which is %f frequency%n", 
      number[i], sides[i], freq);
}
于 2014-08-15T05:25:47.300 回答
0

跟踪掷骰子的次数以及您寻找的结果的掷骰次数。然后你可以做

if(thisRoll/totalRolls >= .25)
    doSomeStuff();

只要确保至少有一个变量有小数点,否则你将有 int/int 除法并且总是得到 1 或 0。

于 2014-08-15T05:19:54.153 回答