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

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

1. ## 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?

2. ## 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. ## 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. ## 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. ## 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?

6. ## 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. ## 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. ## 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) " );

if ( answer.equalsIgnoreCase( "N" ) )
{
again = false;
}
}

System.out.println( "Thanks for playing!" );

} // end method main()

} // end class WhileLoopExample```

9. ## 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

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. ## Re: Need help with YES/NO Loop in Fibonacci Sequence

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

11. ## Re: Need help with YES/NO Loop in Fibonacci Sequence

Nah... this actually quite helped.