I have a method that is called that returns the value of 2 dice rolls, one for the computer and one for the player.
I have an array that has all the possible values of dice rolls.
I want to compare the values of the rolls to the values in the array and then declare the winner by whoever has the lowest array index value. This where the problem occurs. I tried to compare the value of the score to the array this way ::::: scoreOrder[playerScore] < scoreOrder[compScore] ::::: but this doesn't work.
The error I receive is: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: and then the value of the score.
All the comparative logic in my If statement needs to be corrected.
This is what I have so far:
import java.util.*; public class Program08 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); //variables int playerScore = 1; int compScore = 1; playerScore = diceRoll(playerScore); compScore = diceRoll(compScore); int scoreOrder[] = {21,66,55,44,33,22,11,65,64,63,62,61,54,53,52,51,43,42,41,32,31}; if (scoreOrder[playerScore] > scoreOrder[compScore]) { System.out.println("player wins"); } else if (scoreOrder[playerScore] < scoreOrder[compScore]) { System.out.println("computer wins"); } else { System.out.println("tie"); } } public static int diceRoll(int x) { double die1 = (int) (Math.random()*6+1); double die2 = (int) (Math.random()*6+1); System.out.println(die1); System.out.println(die2); x = (int) Math.max(die1, die2)*10 + (int) Math.min(die1, die2); System.out.println("roll is: " + x); return x; } }