我是编程新手,在 eclipse 中运行一些新代码时,我遇到了这个错误,并且完全迷失了。
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;
}
}
问题似乎出在线上if(totalScores[i]>=totalScores[maxIndex])
你有一个;
在你最后一次之后for
,所以之后for
在每个步骤中执行时无需附加命令,变量i
将会有价值students.length
它超出了数组的范围。然后{ ... }
for 后面的块执行一次,最终值为i
,导致异常。
删除那个;
它应该有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)