私は配列に5つの乱数を割り当て、昇順で並べ替えるプロジェクトに取り組んでいますが、次のエラーが出ます....私はどんな助けにも感謝します。 j = 0
スレッド "main"の例外java.lang.ArrayIndexOutOfBoundsException:4エラー
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;
}
}
}
}
}
5個の数値を格納するには 'public static int [] dice = new int [5];が必要です。 –
実際のエラーが何であるか貼り付ける必要があります:) – muzzlator
そして、 'roll'の' for'ループの外側に 'sort(dice)'を入れたいとします。 1行下に移動します。 –