my assignment is to code a program to simulate the rolling of 2 dice. I have most of it set but when I run it and have it print out how many times each number (2-12) was rolled, it displays the numbers 1 off. for instance it will display the number of 4s rolled next to the 5s.
here it is
import java.util.Scanner; //allows info to be inputted// import java.util.Random; //opens random number generator// public class RollTheDiceWithArray13 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); //telling program that information will be gathered from what the user inputs// System.out.println("Welcome to the CS1400 Dice Simulator"); //Welcome user// int numberRolls; //Preparing to receive int// int numberAsterisk = 0; System.out.print("How many times would you like to play?"); //Asking for the int// numberRolls = stdIn.nextInt(); //storing the int// int[] tally = new int[13]; // array for count of rolled dice for(int i=0; i<numberRolls; i++) //setting up conditions to repeat actions// {int dieOne = new Random().nextInt(6) + 1; //setting up variable for first die// int dieTwo = new Random().nextInt(6) + 1; //setting up variable for second die// int diceTotal = dieOne+dieTwo; //setting up variable for sum of the dice// System.out.println("score on the roll " + diceTotal); //printing results(mainly for my benefit to see if the program was working)// tally[diceTotal]++; //Keeping rolling stotal of score on dice.// } for (int i = 2; 1 < tally.length; i++) System.out.println((i+1) + ": " + tally[i]); //printed tally counts// } }
here is the error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 13
at RollTheDiceWithArray13.main(RollTheDiceWithArray13 .java:36)