I'm trying to write a java program that asks the user to enter in the amount of dice to roll and the amount of times to roll the dice.
The program that I wrote so far isn't rolling the dice properly: if I roll 2 dice, then 2 and 24 should have the least amount of rolls and the middle numbers would have the most amount of rolls. Right now it rolls each number pretty evenly.
Does anyone know how I can fix this?
import java.util.Random;
import java.util.Scanner;
public class DiceStats {
public static Random rand= new Random();
public static int rollDice() {
int dice;
dice=(rand.nextInt(6)+1);
return dice; // returns the sum dice rolls
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("How many dice will constitute one roll? ");
int diceNumber = input.nextInt();
System.out.print("How many rolls? ");
int rollNumber = input.nextInt();
int[] frequency = new int[6*diceNumber+1];
for (int i = 0; i < rollNumber; i++) {
++frequency[(rollDice()*diceNumber)];
}
System.out.printf("%s%10s\n", "Face", "Frequency");
for (int face= diceNumber; face <frequency.length; face++)
System.out.printf("%4d%10d\n", face, frequency[face]);
}
}
diceNumber. You need to actually rolldiceNumbertimes (i.e. with another loop). - nobody