How to write Fibonacci Java programs without using if
•
Java
What is the code written in int Fibonacci (int n) instead of using "if" Java recursive Fibonacci sequence as they do here?
This is the program I tried to write:
public class Fibonacci { public static void main(String[] args) { int f = 0; int g = 1; for(int i = 1; i <= 10; i++) { f = f + g; g = f - g; System.out.print(f + " "); } System.out.println(); } }
Solution
Your program is completely correct; What you need to change is the location of the print statement:
public static void main(String[] args) { int f = 0; int g = 1; for(int i = 1; i <= 10; i++) { System.out.print(f + " "); f = f + g; g = f - g; } System.out.println(); }
Alternatively, print g instead of F
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码