我不明白为什么在运行程序后总是出现数组越界错误。
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/