我的程序将无法访问二维数组的第二行。 ArrayIndexOutofBoundsException

时间:2019-04-10 01:43:56

标签: java arrays multidimensional-array

在此程序中,我尝试获取分数并将其分类为范围,然后告诉您在该特定范围内有多少分数。我使用第1行保存分数,然后使用第2行存储每个范围的金额。

//Declarations
int[][] scores = new int[2][26];
int x = 0;
int one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0;

System.out.print(scores[2][1]);

while (inFile.hasNext())
{
     scores[1][x] = inFile.nextInt();  //<---- Putting the numbers into the arrayh which works fine
     x++;
}

x = 0;
while (x < 20)
{
     scores[2][x] = 0;      //<--- Seeing if I had to populate this part of the array  ???THIS IS WHERE IT ERRORS???
     x++;
}


x = 0;
while (x < scores[1].length)
{
System.out.print(scores[2][x] + " ");           //<--- This is me making sure that it puts zeros where I want them.
x++;
}
System.out.println();
x = 0;


while (x < scores.length)
{
    if (scores[1][x] > 0 && scores[1][x] < 24)
    {
        scores[2][1] = one + 1;
        one++;
    }

    else if (scores[1][x] > 25 && scores[1][x] < 49)
    {
        scores[2][2] = two + 1;
        two++;
    }
    else if (scores[1][x] > 50 && scores[1][x] < 74)
    {
        scores[2][3] = three + 1;
        three++;
    }
    else if (scores[1][x] > 75 && scores[1][x] < 99)    
    {
        scores[2][4] = four + 1;
        four++;
    }
    else if (scores[1][x] > 100 && scores[1][x] < 124)
    {
        scores[2][5] = five + 1;
        five++;
    }
    else if (scores[1][x] > 125 && scores[1][x] < 149)
    {
        scores[2][6] = six + 1;
        six++;
    }
    else if (scores[1][x] > 150 && scores[1][x] < 174)
    {
        scores[2][7] = seven + 1;
        seven++;
    }
    else if (scores[1][x] > 175 && scores[1][x] < 200)
    {
        scores[2][8] = eight + 1;
        eight++;
    }
}

我希望它的scores [1] [x]填充有测试分数,然后让scores [2] [x]填充零,然后递增以计算其中有多少分数每个范围

0 个答案:

没有答案
相关问题