Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Need help with YES/NO Loop in Fibonacci Sequence

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with YES/NO Loop in Fibonacci Sequence

    I need help with my Fibonacci Sequence Code:

     
    public class FibSeqByIanNeumann {
     
        public static void main(String[] args) {
     
            Scanner get = new Scanner(System.in);
     
            int ctr, num1, num2, fib, maxTimes;
     
            System.out.print("How many sequences do you want?: ");
            maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
     
            num1 = 0; //This is the part to get the fib sequence to start at 0!
            num2 = 1; //helps to add up the fib sequence numbers.
            fib = 0;
     
            System.out.print("\n" + "When a fibonacci sequence runs for " + maxTimes
                    + " times, it looks like this: " + "\n");
     
            System.out.print("\n" + num1);
            System.out.print(", " + num2);
     
            for (ctr = 0; ctr <= maxTimes - 2; ctr++) 
            {
     
                fib = num1 + num2;
                num1 = num2;
                num2 = fib;
     
                System.out.print(", " + fib);
     
            }
     
            System.out.print("\n" + "\n");
     
        }
    }

    I want to know how to do a simple YES/NO Loop so I can try to have it work on my code if I want to try to do the Fibonacci Sequence again.

    Now I think it might have something to do with a do/while loop, but I'm not sure how it's supposed to work exactly.

    Can anyone show me how it works exactly?
    Last edited by LooneyTunerIan; March 23rd, 2014 at 05:28 PM. Reason: Editing the title so it makes more sense


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Using a while loop, the general form is:
    while ( again )
    {
        // the code to repeat
     
     
        // ask the user if the loop should repeat,
        // if yes, again stays true
        // if no, again = false.
     
    }
    The same can be done with a do/while loop.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Yeah...

    I heard of this, but...

    How can I make it work for a YES/NO Loop?

    If I choose yes, how will it do it again?

    I'm very confused.

    Can anyone show me how?

    You can use my code if you want for example.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    There's no such thing as a "YES/NO Loop" in Java.
    If I choose yes, how will it do it again?
    By using an if statement to set a flag ('again' in my example) that indicates whether the loop will repeat. My example does everything but write the code for you. Why don't you try filling in the code and then come back with code when you need help.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Hmm.. so what you're saying is... if I do it like this:

    while (again = true)
            {
     
            int ctr, num1, num2, fib, maxTimes;
     
            System.out.print("How many sequences do you want?: ");
            maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
     
            num1 = 0; //This is the part to get the fib sequence to start at 0!
            num2 = 1; //helps to add up the fib sequence numbers.
            fib = 0;
     
            System.out.print("\n" + "When a fibonacci sequence runs for " + maxTimes
                    + " times, it looks like this: " + "\n");
     
            System.out.print("\n" + num1);
            System.out.print(", " + num2);
     
            for (ctr = 0; ctr <= maxTimes - 2; ctr++) 
            {
     
                fib = num1 + num2;
                num1 = num2;
                num2 = fib;
     
                System.out.print(", " + fib);
     
            }
     
            System.out.print("\n" + "\n");
     
            System.out.print("Would you like to try again? Click 1 for Yes or 2 for No");
     
            }
        }
    }

    The program will repeat? Is that correct?
    Last edited by LooneyTunerIan; March 23rd, 2014 at 02:24 PM.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Close.
    while (again = true)
    '=' is an assignment operator. Further, a comparison using a boolean is unnecessary simply:
    while ( again )
    will do (as I showed originally).

    Then,
    System.out.print("Would you like to try again?");
    You already have a Scanner object with which to collect input from the user/console. Do that to get the answer to the above question, then use the if statement I described to set the 'again' flag.

  7. #7
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    This is getting very confusing.

    Please... can you show me how to do just that by using my code?

    Not just show me the little parts?

    Here's another helpful tip: I want to be able to input 1 for YES or 2 for NO.

  8. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: [UNSOLVED] Need help with YES/NO Loop in Fibonacci Sequence

    Little details like using 1 or 2 instead of YES/NO are fundamental to the design, not helpful tips.

    This is nothing you can't do, but here's an elementary example. You can figure out how to change the code to use 1/2 versus Y/N:
    import java.util.Scanner;
     
    // a simple class to demonstrate a while loop that repeats
    // according to the user's selection
    public class WhileLoopExample
    {
        public static void main( String[] args )
        {
            boolean again = true;
            Scanner getInput = new Scanner( System.in );
     
            while ( again )
            {
                // add code to repeat here
                System.out.println( "In the loop." );
     
                // gathers the user's input to determine if the loop
                // should repeat. does NOT include input verification or any
                // other error checking
                System.out.print( "Would you like to calculate another"
                    + " value? (Y/N) " );
                String answer = getInput.nextLine();
     
                if ( answer.equalsIgnoreCase( "N" ) )
                {
                    again = false;
                }
            }
     
            System.out.println( "Thanks for playing!" );
     
        } // end method main()
     
    } // end class WhileLoopExample

  9. #9
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: [UNSOLVED] Need help with YES/NO Loop in Fibonacci Sequence

    Thank you.

    I will try it out.

    --- Update ---

    Sorry for double posting, but...


     
    public class FibSeqByIanNeumann {
     
        public static void main(String[] args) {
     
            Scanner get = new Scanner(System.in);
            Scanner input = new Scanner( System.in );
            boolean again = true;
            int ctr, num1, num2, fib, maxTimes;
     
            while ( again ) //loops again if user inputs Y for yes.
            {
     
            System.out.print("\n" + "How many sequences do you want?: ");
            maxTimes = get.nextInt(); //inputs the maxium limit of the fib sequence
     
            num1 = 0; //This is the part to get the fib sequence to start at 0!
            num2 = 1; //helps to add up the fib sequence numbers.
            fib = 0;
     
            System.out.print("\n" + "When a fibonacci sequence runs for " + maxTimes
                    + " times, it looks like this: " + "\n");
     
            System.out.print("\n" + num1);
            System.out.print(", " + num2);
     
            for (ctr = 0; ctr <= maxTimes - 3; ctr++) 
            {
     
                fib = num1 + num2;
                num1 = num2;
                num2 = fib;
     
                System.out.print(", " + fib);
     
            }
     
            System.out.print("\n" + "\n");
     
                System.out.print("Would you like to try another sequence?" + "\n"); //asks user for another sequence.
                System.out.print("Input Y for Yes or N for No: "); //information for user
                String answer = input.nextLine(); //inputs answer for YES or NO.
     
                if ( answer.equalsIgnoreCase( "N" ) ) //If the answer is N or no...
                {
                    again = false;
                }
     
            }
     
            System.out.println("\n" + "''That's all Folks!''" + "\n");  //...this happens.
     
        } 
    }

    It is... done.

    I'll leave this code up for future references.

  10. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Oh (disappointed). You couldn't figure out how to do 1/2?

  11. #11
    Junior Member
    Join Date
    Mar 2014
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Nah... this actually quite helped.

  12. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Need help with YES/NO Loop in Fibonacci Sequence

    Okay then. Glad to help.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM

Tags for this Thread