java - 编译后数组越界错误

标签 java arrays

我不明白为什么在运行程序后总是出现数组越界错误。

import java.util.Scanner;
public class Judge {

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a degree of difficulty between 1.2 - 3.8");
    String difficultyString = keyboard.nextLine();
    double difficulty = Double.parseDouble(difficultyString);
    int[] scores = new int[6];
    for(int i = 0; i<scores.length; i++){
        System.out.println("Enter the score of a judge");
        String tempWord = keyboard.nextLine();
        int temp = Integer.parseInt(tempWord);
        scores[i] = temp;
    }
    int low = Math.min(Math.min(Math.min(Math.min(scores[0], scores[1]), Math.min(scores[2], scores[3])),Math.min(scores[4], scores[5])), scores[6]);

    int high = Math.max(Math.max(Math.max(Math.max(scores[0], scores[1]), Math.max(scores[2], scores[3])),Math.max(scores[4], scores[5])), scores[6]);
    int additon = scores[0] + scores[1] + scores[2] + scores[3] + scores[4] + scores[5] + scores[6];
    int score = (additon - low) - high;
    double actualScore = (score * difficulty) * 0.6;
    System.out.println("The score of the diver is : " + actualScore);
}

}

忽略我的代码。我知道这很草率,而且不是最简单的方法,但我到底做错了什么?

最佳答案

您的 scores 数组的长度为 6,这意味着 0 到 5 个索引,6 个元素。您正在访问它的第六个索引。您最多只能添加第 5 个索引,如下所示

int additon = scores[0] + scores[1] + scores[2] + scores[3] + scores[4] + scores[5];

关于java - 编译后数组越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23074694/

相关文章:

sql - 如何查询 postgres json 列中的嵌套数组?

c# - 如何在 C# 中计算非常大的 double 组的轮数

arrays - 使用 lodash 从对象数组创建唯一数组时出现类型错误

java - Tomcat和防洪

java - 尝试在 Java 中构建

java - Java中使用数组计算数据表中行的平均值

java - 旧程序中的四元数旋转

javascript - 使用 window.open(url ,"_self") 打开下载窗口,但不打开 GET 请求

arrays - 你能在快速排序中选择你想要的任何主元吗?

javascript - 在javascript中创建一个对象数组,然后获取属性