Java的哈利指数外边界的异常 [英] Java Arry Index Out of Bound Exception

查看:141
本文介绍了Java的哈利指数外边界的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力,当我需要5用户输入的值保存到一个数组,将其发送到的方法,并找到和显示的最低值这个基本的Java程序。

该程序是很简单的,它运行,但是当我进入最后一个号码,我得到的错误:

异常线程mainjava.lang.ArrayIndexOutOfBoundsException:4
        在minNumber.main(minNumber:14)

帮助?

 进口的java.util。*;类minNumber {
    公共静态无效的主要(字符串ARGS []){    扫描仪输入=新的扫描仪(System.in);    INT numberArray [] =新INT [4];
    INT findLowest;    的for(int i = 0; I< = numberArray.length;我++){
        的System.out.println(插槽输入一个值+(I + 1)+:);
        numberArray [I] = input.nextInt();
    }
    findLowest = getMin(numberArray);
    displayOutput(findLowest);
}公共静态INT getMin(INT NUM []){INT lowestNum = 0;
对于(INT J = 0; J< = num.length; J ++){
    如果(NUM [J]< NUM [J + 1]){        lowestNum = NUM​​ [J]。
    }
}
返回lowestNum;
}公共静态无效displayOutput(INT最低){的System.out.println(最小的数字是:+最低);
}
}


解决方案

首先,如果你想在一个数组5个值,然后用5声明它:

  INT numberArray [] =新INT [5];

其次,你会关闭阵列的末尾。更改

 的for(int i = 0; I< = numberArray.length;我++){

 的for(int i = 0; I< numberArray.length;我++){

您将需要更改其他Ĵ 循环这种方式也。

顺便说一句,你的 getMin 方法需要除了我上面提到的变再变,因为他说 NUM [J + 1] 仍将跑出数组的结尾,即使你对上面的变化。我想你会需要将当前数组元素与比较 lowestNum ,而不是一个数组元素。

I have been working on this basic java program when I need to store 5 user entered values into an array, send it to a method, and find and display the lowest value.

The program is simple enough, and it runs, but when I enter the last number, I get the error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4 at minNumber.main(minNumber:14)

Help?

import java.util.*;

class minNumber {


    public static void main(String args[]){

    Scanner input = new Scanner(System.in);

    int numberArray[] = new int[4];
    int findLowest;

    for (int i = 0; i <= numberArray.length; i++){
        System.out.println("Enter a value for slot "+(i+1)+ ":");
        numberArray[i] = input.nextInt();   
    }
    findLowest = getMin(numberArray);
    displayOutput(findLowest);
}

public static int getMin(int num[]){

int lowestNum = 0;
for (int j = 0; j <= num.length; j++){
    if (num[j] < num[j+1]){

        lowestNum = num[j];
    }
}
return lowestNum;
}

public static void displayOutput(int lowest){

System.out.println("The lowest number is: "+lowest);
}
}

解决方案

First, if you want 5 values in an array, then declare it with 5:

int numberArray[] = new int[5];

Second, you are going off the end of the array. Change

for (int i = 0; i <= numberArray.length; i++){

to

for (int i = 0; i < numberArray.length; i++){

You'll need to change your other j for loop this way also.

As an aside, your getMin method needs another change besides the change I mentioned above, because saying num[j+1] will still run off the end of the array even if you make the change above. I think you'll need to compare the current array element versus lowestNum, not the next array element.

这篇关于Java的哈利指数外边界的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆