Thread: fibonacci sequence View Single Post
 04-17-2013, 09:30 PM PM User | #2 Fou-Lu God Emperor     Join Date: Sep 2002 Location: Saskatoon, Saskatchewan Posts: 16,603 Thanks: 4 Thanked 2,598 Times in 2,567 Posts Lets start in main. The first thing you do is ask for the number, 1 - 9 inclusive. You then scan that number in. Assign this number to two new variables, iStart and iCurrent. Set iLast to 0. Next, there are two pretty much un-needed methods: nextNumber() and repeat(). You can do these both very easily within responce(). Just takes a little thought on which variables to use for the class. Use these for your properties: PHP Code: ``` private static Scanner in;private static int iStart = 0;private static int iLast = 0;private static int iCurrent = 0;  ``` Start is your start number, and only required if you actually want to do something after the fact with it. Current is the current number, and last is the previous. Since the next is always current + last, there is no need to calculate with looping. PHP Code: ```         public static void responce()        {            String sContinue = "";            int iNext = 0;            do            {                sContinue = "";                iNext = iLast + iCurrent;                System.out.println(iNext + "\n");                iLast = iCurrent;                iCurrent = iNext;                System.out.print("Would you like the next number as well?");                sContinue = in.nextLine().trim();            }            while (sContinue.equalsIgnoreCase("yes") || sContinue.equalsIgnoreCase("y"));        }  ``` I'd also override the responce to accept an integer for the next n values. That way I can simply ask for the next x numbers to show without having to get input. That would then separate the internal handling of the loop and put it into its own method to which both methods can call it. After that, assemble the main to loop for more. That gives me an end class of: PHP Code: ``` import java.util.Scanner;public class fibseq{    private static Scanner in;    private static int iStart = 0;    private static int iLast = 0;    private static int iCurrent = 0;    public static void main(String[] args)    {        String sContinue = "";        in = new Scanner(System.in);        do        {            sContinue = "";            getNumber();            responce();            System.out.print("Would you like to try another number? ");            sContinue = in.nextLine().trim();        }        while (sContinue.equalsIgnoreCase("yes") || sContinue.equalsIgnoreCase("y"));        in.close();        System.out.println("Goodbye.");    }    public static void getNumber()    {                System.out.print("Enter your number(single digit 1 - 9 no 0): ");        iStart = iCurrent = in.nextInt();        iLast = 0;        in.nextLine();    }    public static void responce()    {        String sContinue = "";        int iNext = 0;        do        {            sContinue = "";            iNext = iLast + iCurrent;            System.out.println(iNext + "\n");            iLast = iCurrent;            iCurrent = iNext;            System.out.print("Would you like the next number as well?");            sContinue = in.nextLine().trim();        }        while (sContinue.equalsIgnoreCase("yes") || sContinue.equalsIgnoreCase("y"));    }}  ``` Note that sContinue is always compared with .equals or .equalsIgnoreCase. You cannot reliable use == comparisons with objects in Java (for string values that is). Strings are immutable, so "y" == "y", but new String("y") == "y" is not true. Fibonacci can of course be designed iteratively or recursively as well (which is the whole point of it), but for simple terms of fibonacci growth I'd suggest just knowing the current and previous is sufficient. I would write the actual method to recurse or iterate it to get the sequence only so it can be extended to tri and tetranocci numbers without needing to change much code. __________________ PHP Code: ``` header('HTTP/1.1 420 Enhance Your Calm');  ``` Last edited by Fou-Lu; 04-17-2013 at 09:33 PM..