-2

I created a code for Fibonacci numbers, but it doesn't compile and I don't know why, can someone help. (And I also think that the last like with k-1 + k-2 doesn't make any sense.

public class Fibonacci {

  public static void main(String[] args) {
    int n = Integer.parseInt(args[n]);

    System.out.println(FibNum(n));
  }

  public static int FibNum(int k) {
    if (k == 0) return 0;
    if (k == 1) return 1; else return k - 1 + k - 2;
  }
}

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
Rex
  • 1

1 Answers1

0

int n = Integer.parseInt(args[n]); how could it work? variable n in not initialized

Qwer Izuken
  • 599
  • 2
  • 6
  • I initialized it in the Arguments, or doesnt it work like that? – Rex Nov 29 '20 at 15:17
  • 1
    @Rex: no, this is not how Java works, and your asking this suggests that you will want to read some basic tutorials that cover use of variables, since this core concept needs to be understood well to progress. – Hovercraft Full Of Eels Nov 29 '20 at 15:19
  • @Rex just get args[0] instead of args[n], it will be your first command-line argumment – Qwer Izuken Nov 29 '20 at 15:24