异常螺纹"主" java.lang.ArrayIndexOutOfBoundsException [英] Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
本文介绍了异常螺纹"主" java.lang.ArrayIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来编程和运行在Eclipse中一些新的code,同时,我碰到这个错误来了,我完全失去了。
I am new to programming and while running some new code in eclipse, I came across this error and am completely lost.
import java.util.Scanner;
public class Lab6
{
public static void main(String[] args)
{
// Fill in the body according to the following comments
Scanner in= new Scanner(System.in);
// Input file name
String FileName=getFileName(in);
// Input number of students
int numOfStudents = FileIOHelper.getNumberOfStudents(FileName);
Student students[] = getStudents(numOfStudents);
// Input all student records and create Student array and
// integer array for total scores
int[]totalScores = new int[students.length];
for(int i=0; i< students.length; i++)
{
for(int j=1; j<4; j++)
{
totalScores[i]= totalScores[i]+students[i].getScore(j);
}
}
// Compute total scores and find students with lowest and
// highest total score
int i;
int maxIndex =0;
int minIndex =0;
for(i=0; i<students.length; i++);
{
if(totalScores[i]>=totalScores[maxIndex])
{
maxIndex=i;
}
else if(totalScores[i]<=totalScores[minIndex])
{
minIndex=i;
}
}
问题似乎是在该行如果(totalScores [I]> = totalScores [maxIndex])
推荐答案
您有一个;
后您的最后一个为
,所以
在每一步没有额外的命令执行后,变量 I
将具有值 students.length
这是数组的边界之外。然后按照对与那终值我
执行一次 {...}
块,导致异常
You have a ;
after your last for
, so after the for
executes with no additional commands in each step, the variable i
will have the value students.length
which is outside the bounds of the array. Then the { ... }
block following the for is executed once with that final value of i
, causing the exception.
删除;
,它应该工作
这篇关于异常螺纹&QUOT;主&QUOT; java.lang.ArrayIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文