Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 0 error in Java

前端 未结 8 2095
清酒与你
清酒与你 2021-01-07 05:52

When i tried running this code I get this error..I dont know where i went wrong..

Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: 0
           


        
8条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-07 05:55

    args[0] will never be null (when invoked from the command line) - but args.length may be 0, in which case evaluating args[0] will give you that exception, because there is no element 0 in the array. Just test for that:

    if (args.length != 0)
    {
        n = Integer.parseInt(args[0])
    }
    

    As an aside, it's pretty odd to return a double from fibo - the normal Fibonacci sequence is defined in terms of integers (0, 1, 1, 2, 3, 5, 8 etc). If you want to scale it, I'd multiply it by your constant afterwards.

提交回复
热议问题