I'm supposed to write a program to figure out the nth number in the fibonacci sequence. My book gave us the main method to run:
import java.util.Scanner;
public class FibonacciRunner
{
public static void main (String[] args)
{
Scanner in = new Scanner (System.in);
System.out.println ("Enter n: ");
int n = in.nextInt();
FibonacciGenerator fg = new FibonacciGenerator();
for (int i = 1; i <= n; i++);
System.out.println(fg.nextNumber());
}
}
and we're supposed to write a class with a constructor anda method to make it work. So far this is what I've got:
import java.util.Scanner;
public class FibonacciGenerator
{
private int a;
private int b;
public FibonacciGenerator()
{
a=0;
b=1;
}
public int nextNumber()
{
//I don't know what to put here??
}
}
the nextNumber() method is supposed to calculate the next number in the sequence, but I have no idea how to calculate it...help?
Question
Kev_B
Hey guys,
I'm supposed to write a program to figure out the nth number in the fibonacci sequence. My book gave us the main method to run:
import java.util.Scanner; public class FibonacciRunner { public static void main (String[] args) { Scanner in = new Scanner (System.in); System.out.println ("Enter n: "); int n = in.nextInt(); FibonacciGenerator fg = new FibonacciGenerator(); for (int i = 1; i <= n; i++); System.out.println(fg.nextNumber()); } }and we're supposed to write a class with a constructor anda method to make it work. So far this is what I've got:
import java.util.Scanner; public class FibonacciGenerator { private int a; private int b; public FibonacciGenerator() { a=0; b=1; } public int nextNumber() { //I don't know what to put here?? } }the nextNumber() method is supposed to calculate the next number in the sequence, but I have no idea how to calculate it...help?
Link to comment
Share on other sites
19 answers to this question
Recommended Posts