简体   繁体   English

我写了斐波那契数列的代码,但出现错误ArrayIndexOutofBoundsException。 你能帮我么

[英]i wrote the code for fibonacci series but i get error ArrayIndexOutofBoundsException. can you please help me

I wrote the code for Fibonacci series but I get error ArrayIndexOutofBoundsException . 我写了斐波那契数列的代码,但出现错误ArrayIndexOutofBoundsException Can you please help me find source of this exception? 您能帮我找到此异常的来源吗?

class Fib {
    public static void main(String args[]) {
        int num = Integer.parseInt(args[0]);
        System.out.println("Fibonacci Series");
        int f1, f2 = 0, f3 = 1;
        for (int i = 1; i <= num; i++) {
            System.out.print(" " + f3 + " ");
            f1 = f2;
            f2 = f3;
            f3 = f1 + f2;
        }
    }
}

You didn't supply any command-line arguments to your program, so args is a zero-length array. 您没有向程序提供任何命令行参数,因此args是零长度的数组。 Any access of a zero-length array will result in an ArrayIndexOutOfBoundsException . 零长度数组的任何访问都将导致ArrayIndexOutOfBoundsException

Check if args.length is at least 1 before accessing the first element (of index 0 ). 在访问第一个元素(索引为0 )之前,请检查args.length是否至少为1

I haven't tested the code you submitted but here is couple of clues: 我尚未测试您提交的代码,但以下是一些提示:

  • You are executing the application without any argument. 您正在执行应用程序而没有任何参数。
  • You should first check if the arguments length is greater than 0. 您应该首先检查参数长度是否大于0。
  • Before parsing the input argument, do either check if it is a number or surround the parsing with a try/catch. 在解析输入参数之前,请检查它是否为数字或使用try / catch包围解析。

I hope this helps. 我希望这有帮助。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 您能帮我提供这个代码吗? - Can you please help me with this code? 我得到错误:可靠的精度损失,我试图改变它,但我可以解决它,请帮助我 - I get the error: posible loss of of precision and i have tried to change it but I can fix it, please help me 有人可以帮助我编写我正在编写的代码吗? (爪哇) - Can someone please help me with the code I am writing? (Java) 您能帮我找出此代码中的错误吗? 我似乎不明白为什么它不起作用? - Can you help me identify the error in this code? I dont seem to understand why it isn't working? 当我编写此代码时,无法在 java 中打开框架视图请帮我修复 - Can't Open Frame view in java when i am Write this Code Please Help me to fix 谁能帮我纠正此代码? - Can anyone please help me correct this code? 我的代码中出现了ArrayIndexOutOfBoundsException - I get an ArrayIndexOutOfBoundsException in my code 我尝试制作一个 Tablayout 但它有一个错误请帮帮我 - I try to make A Tablayout But its have an error Please Help me 我不知道为什么会出现此错误,请帮助我 - I dont know why this error occurs, please help me 我试图用 android studio 发布一个应用程序,但它给了我这个错误你能帮我吗? - I was trying to release an app with android studio but it gave me this error can you help me?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM