Hey !
I'm a fresh java programmer and doing some exercises from a book.
Got one that requires me to make a program that would read a number, and then give the coressponding value from Fibonacci's series (Without vectors/arrays), using the while function.
I've managed to get smt done, but the computer just waits for smt... I normaly would pursue the solution myself in order to learn smarter/better, but there is no error on compiling, or runtime.
A trained eye will spot the bug in a giff :
Code:
import java.io.*;
public class fibonacci {
public static void main(String [] args){
int fa = 1;
int fb = 1;
int fc = 0;
int n = 0;
BufferedReader tastatura = new BufferedReader(new InputStreamReader(System.in),1);
String linie = "";
try {
System.out.println("Which Fibonnacci number shall it be?");
linie = tastatura.readLine();
n = Integer.valueOf(linie);
int i=2;
while (i <= n){
fc = fa+fb;
fb = fa;
fa = fc;
}
System.out.println("the "n" number: " + n + "corresponds to: " + fc);
}
catch (IOException e)
{
System.out.println("error: " + e.toString());
}
}
}
I know there is a smarter way to do this, but wanna try to get the juices out of my unevolved brain 1st.
The 'algorithm' was simple. Have 2 variables hold the initial values (global), outside the while loop, then just increment(change) each, based on the other's value(evolution).
Should work in theory....
I think it has something to do with using variables outside the loop, and writing values in variables outside the loop. I think it shouldn't be a problem (for me), but I'm sure that someone would give me a more than logical/reasonable explanation for not doing this.