My assignment is supposed to simulate a running total of a dice sum depending on how many dice are used and how many rolls the user wants. The sample output shows that the percentage of each sum occurring varies a lot, however, the frequency of mine are all around the same percentage. I'm not sure how to fix this.
package project1;
import java.util.Scanner;
import java.util.Random;
public class DiceStats {
public static void main(String[] args) {
int diceCount;
int rolls;
int min;
int max;
// Ask user how many dice
System.out.print("How many dice will constitute one roll? ");
Scanner dice = new Scanner(System.in);
diceCount=dice.nextInt(); //store it into diceCount
min = diceCount; //minimum sum
max = diceCount*6; //maximum sum
int[] diceSum = new int[max+1]; //initialize array size
System.out.print("How many rolls? ");
//Ask user how many rolls
Scanner roll = new Scanner(System.in);
rolls=roll.nextInt();
Random sum= new Random(); //random sum generator
System.out.printf("\n%s%10s%10s\n", "Sum", " # of times", " Percentage");
//for loop to sum up # of times
for (int i =1; i<= rolls; i++)
{
++diceSum[sum.nextInt((max-min)+1)+min];
}
//for loop to print out: sum, # of times, and percentage
for (int i = min;i <= (diceSum.length-1); i++)
{
double percentage = (double)diceSum[i]/rolls;
System.out.printf("%d %d %.2f%%\n", i, diceSum[i],percentage*100);
}
}
}
This is how my program is being outputted: my output
EDIT: This is the sample output I am comparing mine to: sample output
Aucun commentaire:
Enregistrer un commentaire