Home > Net >  what is wrong with this simple fibo code?
what is wrong with this simple fibo code?

Time:01-19

import java.util.*;

public static void main(String args[]){ 
    
Fibo();

Class Fibo(){

    int n1=0, sum;

    for(int i =0; i<10;  i   ){
  
    sum = i   n1;
    n1=n1 sum;
    System.out.println("" sum);
    }  
}
}

All want to do is print the first 10 fibo numbers and then add functionality to tell the computer which fibo numbers I want to print. Say like the 1 millionth fibo number.

CodePudding user response:

That is genuinely some of the worst Java I've ever had the displeasure of seeing.

Use this instead:

public class Main {
    public static void main(String[] args) {
        int sum = 0;

        for (int i = 0; i < 10; i  ) {
            sum  = i;
            System.out.println(sum);
        }
    }
}

The output is:

0
1
3
6
10
15
21
28
36
45

CodePudding user response:

So let's get to the problems of your provided code:

1. Syntax of defining a Class

To define a class in Java, it should be something like this:

class Fibo {

}
  1. There should be no () at the end of Fibo.
  2. You should use a lower case c instead of upper case C. That is a keyword in Java and it is case-sensitive.

And sometimes you would see there is a keyword public in front of class:

public class Fibo {

}

With public, you can access the class outside the Package. And I leave it to you if you want to explore more.

2. Always define your functions inside a Class

public static void main(String[] args) {} is a function. And you should always place your function inside a Class.

public class Fibo {
    public static void main(String[] args) {
        // Your main running function
    }
}

3. Proper way to call Function or Class

To call function or create a new Class object:

public class Test {
    public static void main(String[] args) {
        // Below is a function call
        functionTwo();
        // Below is creating a new class, assuming you have another simple Class named ClassTwo with only default constructor
        ClassTwo classTwo = new ClassTwo();
    }
    
    private static void functionTwo() {
        System.out.println("Inside functionTwo");
    }
}

4. Logic of getting your Fibonacci numbers

First of all, here are some Fibonacci numbers:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233

The first number = 0
The second number = 1
The third number = The first number The second number
The fourth number = The second number The third number
... and so on

So according to the above logic, we can write our function to generate Fibonacci numbers:

public class Fibo {
    public static void main(String[] args) {
        int target = 10;
        int firstNumber = 0;
        int secondNumber = 1;
        int nextNumber = 0;
        System.out.println(firstNumber); // Print out the First Number
        System.out.println(secondNumber); // Print out the Second Number
        // Because there is a logic starting from the Third Number
        // Here we use a for loop to repeat until we get to our target 10
        for (int i = 2; i < target; i  ) {
            // Next number is equal to (i-2)   (i-1), where i = current number
            nextNumber = firstNumber   secondNumber;
            // Print out the current number
            System.out.println(nextNumber);
            // As a for loop proceeds to the next one, we also need to update
            // our firstNumber and secondNumber with next one
            firstNumber = secondNumber;
            secondNumber = nextNumber;
        }
    }
}

Output

0
1
1
2
3
5
8
13
21
34

Of course the above program can be simplified and also obtain the same result. But at this stage, having a clearer concept is of utmost importance.

5. Remove unused Import

This is not fatal but as a good practice. Because in your code, you have not made use of any classes from other packages and therefore you do not need to import anything.

import java.util.*;

6. Keep indentation

This is also not fatal but as a good practice. Which of the following code would like to see?

public static void main(String[] args) {
  System.out.println("This is a line");
     int a = 0;
 int b = 10;
  for (int i = 0; i < 10; i  ) {
      System.out.println("Line"   i);
      }
}
public static void main(String[] args) {
    System.out.println("This is a line");
    int a = 0;
    int b = 10;
    for (int i = 0; i < 10; i  ) {
        System.out.println("Line"   i);
    }
}

And to your extra question. This time you have to make use of class that is beyond your package. So you need to make use of import. And that class is Scanner.

So the new full code should be like this:

import java.util.Scanner;

public class Fibo {
    public static void main(String[] args) {
        System.out.print("Enter target: ");
        Scanner scanner = new Scanner(System.in);
        int target = scanner.nextInt();
        int firstNumber = 0;
        int secondNumber = 1;
        int nextNumber = 0;
        System.out.println(firstNumber); // Print out the First Number
        System.out.println(secondNumber); // Print out the Second Number
        // Because there is a logic starting from the Third Number
        // Here we use a for loop to repeat until we get to our target 10
        for (int i = 2; i < target; i  ) {
            nextNumber = firstNumber   secondNumber;
            System.out.println(nextNumber);
            firstNumber = secondNumber;
            secondNumber = nextNumber;
        }
    }
}

Output

Enter target: 10
0
1
1
2
3
5
8
13
21
34
  • Related