控制台在线程“ main”中显示异常。 java.lang.ArrayIndexOutOfBoundsException:0 [英] Console shows Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0

查看:99
本文介绍了控制台在线程“ main”中显示异常。 java.lang.ArrayIndexOutOfBoundsException:0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试运行和编译代码时,控制台将显示:

When I try to run and compile my code, the console shows:

 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at FactorialProgram5.main(FactorialProgram5.java:9)

我可以

这是我的代码:

import java.util.Scanner;

public class FactorialProgram5 {
    public static void main(String args[]) {
        long n;
        long fact = 1;

        n = Long.parseLong(args[0]);

        for (int i = 1; i <= n; i++) {
            fact = fact * i;
        }

        System.out.print("fact=" + fact);
    }
}


推荐答案

因为当您运行程序时,没有将参数传递给它。您的args数组为空,并且当您尝试获取空数组的第0个元素时,您将获得IndexOutOfBoundException
以下行需要参数

Because when you are running your program you are not passing arguments to it. The your args array is empty and when you are trying to get the 0th element of empty array you get IndexOutOfBoundException The following line expects argument

n = Long.parseLong(args [0]);

尝试通过向其传递参数来运行程序

try to run your program by passing arguments to it

这篇关于控制台在线程“ main”中显示异常。 java.lang.ArrayIndexOutOfBoundsException:0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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