Fibbonaci sequence without arrays
Im running into a problem creating this.
So far I can get the code to take a command line argument representing length of the sequence and print that many items
prints 5 numbers.
I can't get it to print anything except 1,1,1,1,1.
Im not looking for an answer, just for someone who is more experienced to look at my code.
My thought for making the code was to use a for loop, looping until cmd arg is met.
if i is 0, or 1, print 1,.
if ta or tb = 0, set them to 1, set another int, named num to ta + tb and print num,
Finally, set tmp to ta, set ta to ta + tb, set tb to tmp, and set num to ta + tb, printing num
The thought is, if i is 3, ta and tb will be set at 1.
so tmp will become 1, then ta will become 2, and tb will become 1, num will be 3.
i = 4:
tmp = 2, ta = 3, tb = 2, num = 5
i=5:
tmp = 3, ta = 5, tb =3, num = 8.
it seems to work, but my code is just spitting out 1's over and over. The only clause that produced print 1 is when i == 0 or i == 1.
heres the code:
Code:
import static net.mindview.util.Print.*;
import java.util.*;
public class Fibb
{
public static void main(String[] args)
{
int tmp = 0;
int ta = 0;
int tb = 0;
int num = 0;
int items = Integer.parseInt(args[0]);
for(int i = 0; i < items; i++)
{
if(i == 0 || i == 1)
{
System.out.print(1 + ", ");
continue;
}
if(ta == 0 || tb == 0)
{
ta = 1;
ta = 1;
num = ta + tb;
System.out.print(num + ", ");
continue;
}
else
{
tmp = ta;
ta = ta + tb;
tb = tmp;
num = ta + tb;
System.out.print(num + ", ");
continue;
}
}
}
}