定义了一个Int,但是需要一个数组?

时间:2019-02-14 12:45:25

标签: java variables int

Image of error;在我的程序中,我试图使用索引按顺序遍历一组数据,并且已将其声明为int,但我不断收到错误消息:“需要数组,但找到int”。

public void calcHurricaneCategory() {
            for (index = 0; index < wind.length; index++) {
                windMPH[index] = wind[index] * mphInKnots;
                totalWind += windMPH[index];
                windAverage += windMPH[index];

                pressureAverage += pressure[index];

                if (windMPH[index] > 74 && windMPH[index] < 95) {
                    category[index] = 1;
                    categoryAverage += category[index];
                    category1++;
                }
                else if (windMPH[index] > 96 && windMPH[index] < 110) {
                    category[index] = 2;
                    categoryAverage += category[index];
                    category2++;
                }
                else if (windMPH[index] > 111 && windMPH[index] < 129) {
                    category[index] = 3;
                    categoryAverage += category[index];
                    category3++;
                }
                else if (windMPH[index] > 130 && windMPH[index] < 156) {
                    category[index] = 4;
                    categoryAverage += category[index];
                    category4++;
                }
                else if (windMPH[index] > 157) {
                    category[index] = 5;
                    categoryAverage += category[index];
                    category5++;
                }
        }   
    }

这是索引的定义方式,

private int index;

在用户实际将index定义为数组之前,我已经看到过类似的问题,但是现在我还是完全感到困惑。

感谢您的帮助

2 个答案:

答案 0 :(得分:2)

您必须将数组声明为

private int[] wind; 
private int[] windMPH; 
private int[] category;

答案 1 :(得分:-4)

更改此行

for (index = 0; index < wind.length; index++)

for (int index = 0; index < wind.length; index++)

或使用

this.index