数组的错误消息 [英] Error message for arrays

查看:113
本文介绍了数组的错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是初学者并且是第一次使用Java数组。当我输出我的代码时,我得到了这个错误。我的实际代码中没有错误,因此我没有看到代码中的问题所在。

I am a beginner and using Java arrays for the first time. When I output my code I get this error. There is no errors in my actual code, so I do not see where the problem is in my code.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0           
at sumdouble.Sumdouble.main(Sumdouble.java:24)

这是我的代码

package sumdouble;


public class Sumdouble {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
 double sum = 0;
 int number = 1;

 double array[] = new double [5];
 for (number  = 0; number < 5; number++)
 {
     array[number] = Double.parseDouble(args[number]);
     System.out.println("The" +number+ "argument value is: " +array[number]);

     for(double numb: array)
     {
         sum = sum + numb;

     }
 }System.out.printf("Sum of all numbers = %2f", sum);
    // TODO code application logic here
}

}


推荐答案

错误发生在循环的第一次迭代中。您访问 args 这是一个充满所谓的命令行参数的数组。如果你没有传递任何,那么该数组是空的,这就是你得到那个错误的原因。

The error happen at the very first iteration of your loop. You access args which is an array that gets filled with the so called command-line arguments. If you don't pass any, that array is empty, and that is why you get that error.

查看官方文档,了解如何传递命令您的Java应用程序的行参数此处。它不会伤害=)

Take a look at the official documentation to learn of to pass command line argument to your java applications here. It cannot hurt =)

如果您正在使用eclipse看看这里

If you are using eclipse take a look here

这篇关于数组的错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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