I am working on a project in which I want to assign 5 random numbers to an array and then sort those numbers in ascending order, but I get the following errors.... I would appreciate any help.
import java.util.Scanner;
public class YahtzeeGame {
public static Scanner sc = new Scanner(System.in);
// random = random between 1.0 and (6 * .999) + 1 is forced to be integer 1-6
public static int random = (int) (Math.random() * 6 + 1);
public static int[] dice = new int[4];
public static void main (String[] args) {
System.out.println("welcome to Yahtzee!");
roll(dice);
}public static void roll (int[] dice) {
for (int i = 0; i < dice.length; i++) {
dice[i] = random;
sort(dice);
}
} public static void sort(int[] dice) {
int temp;
for (int j = 0; j < dice.length - 1; j++) {
for (int i = 1; i < dice.length - j; i++) {
if( dice[i] > dice[i+1]) {
temp = dice[i-1];
dice[i-1] = dice[i];
dice[i] = temp;
}
}
}
}
}
public static int[] dice = new int[5];to store 5 numbers.sort(dice)to be OUTSIDE theforloop inroll. Move it down one line.forloop insortis wrong. I think it should probably bei < dice.length - j - 1. The current version fails whenj = 0.