# Need help with YES/NO Loop in Fibonacci Sequence

• March 23rd, 2014, 12:42 PM
LooneyTunerIan
Need help with YES/NO Loop in Fibonacci Sequence
I need help with my Fibonacci Sequence Code:

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?
• March 23rd, 2014, 01:51 PM
GregBrannon
Re: Need help with YES/NO Loop in Fibonacci Sequence
Using a while loop, the general form is:
Code java:

```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.
• March 23rd, 2014, 01:59 PM
LooneyTunerIan
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. :confused:

Can anyone show me how?

You can use my code if you want for example.
• March 23rd, 2014, 02:06 PM
GregBrannon
Re: Need help with YES/NO Loop in Fibonacci Sequence
There's no such thing as a "YES/NO Loop" in Java.
Quote:

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.
• March 23rd, 2014, 02:08 PM
LooneyTunerIan
Re: Need help with YES/NO Loop in Fibonacci Sequence
Hmm.. so what you're saying is... if I do it like this:

Code Java:

```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?
• March 23rd, 2014, 02:28 PM
GregBrannon
Re: Need help with YES/NO Loop in Fibonacci Sequence
Close.
Code :

`while (again = true)`
'=' is an assignment operator. Further, a comparison using a boolean is unnecessary simply:
Code :

`while ( again )`
will do (as I showed originally).

Then,
Code :

`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.
• March 23rd, 2014, 02:48 PM
LooneyTunerIan
Re: Need help with YES/NO Loop in Fibonacci Sequence
This is getting very confusing. :confused:

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.
• March 23rd, 2014, 03:12 PM
GregBrannon
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:
Code java:

```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```
• March 23rd, 2014, 05:28 PM
LooneyTunerIan
Re: [UNSOLVED] Need help with YES/NO Loop in Fibonacci Sequence
Thank you.

I will try it out.

--- Update ---

Sorry for double posting, but...

Code java:

```  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.
• March 23rd, 2014, 05:39 PM
GregBrannon
Re: Need help with YES/NO Loop in Fibonacci Sequence
Oh (disappointed). You couldn't figure out how to do 1/2?
• March 24th, 2014, 01:10 PM
LooneyTunerIan
Re: Need help with YES/NO Loop in Fibonacci Sequence
Nah... this actually quite helped. :)
• March 24th, 2014, 03:06 PM
GregBrannon
Re: Need help with YES/NO Loop in Fibonacci Sequence
Okay then. Glad to help.