I’m having a little trouble with a problem in my intro to java book. Here is the situation: A user must input a Number, after that I must find the Fibonacci of that number. I am given this equitation for listing the numbers(see code). While yes i got that working i am wondering how i go about calculating my Fibonacci from that. oh and on a side note “Notepad ++” yells at me for not having a “public static void main(Strings[] args)”. Is there a work around for that?
public class Fibonacci
{
public static void main(String[] args)
{
}
public int Fib(int n)
{
FibonacciJDialog userInput = new FibonacciJDialog();
int in1=1,in2=1;
int sum=1;//initial value
int index;
index = userInput.getUserInput();
while (index <= n)
{
sum = in1+in2;// sum=the sum of 2 values;
in1=in2;// in1 gets in2
in2 = sum;// in2 gets sum
index++;// increment index
}
return sum;
}
}
I’m not sure what answer you’re getting, but you should consider doing the user input part in the main function, then passing that into the function as n. Then, in the fib function, you can loop from 0 to n, rather than index to n. Although, that depends on the nature of the problem. For example, if 1 and 1 are the first two numbers of the sequence and you want to return the Fibonacci number at index n, then you should start at index 3 (because sum would be 1+1 = 2, which is the third number in the sequence). Then you can loop until you reach n.