# Thread: "Continue Yes or No" Problem

1. Member
Join Date
Feb 2013
Posts
68
Rep Power
0

## "Continue Yes or No" Problem

Does anybody see what could be wrong? I've found out how to code a "Continue Yes or No", but now when you enter "Yes" & "No" it terminates the program. Whereas if they typed "Yes"; it should just keep going.

Thanks.

Java Code:
```import java.util.Scanner;

public class Fibonocci {

public static void main(String[] args) {
boolean play = false;
String playAgain;

int index;
do
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a value");
index = keyboard.nextInt();
System.out.println("Fibonocci Number: "+ fibonocci(index));
} while (play == true);

/* ===================================================== */

Scanner in = new Scanner(System.in);
System.out.println("Would you like to continue (yes/no)?");

playAgain = in.nextLine();
if(playAgain.equals("yes"))
play = true;

if(playAgain.equals("no"))
{
play = false;
System.out.println("Program is terminated");
}
}
/* ================================================= */

public static long fibonocci(int num)
{
if (num == 0)
return 0;
if (num <= 2)
return 1;

long fibOne = fibonocci(num - 1) + fibonocci(num - 2);
return fibOne;

}
}```

2. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

Your do loop only loops if 'play' equals true. Your initial declaration of 'play' was 'false' so when does this become true?

Regards.

3. Member
Join Date
Feb 2013
Posts
68
Rep Power
0

## Re: "Continue Yes or No" Problem

Wouldn't it become 'true' if the user inputs 'yes' though? It has worked on the other two programs I had to code.

Plus I started it with play = true; thinking that could be it but it skips asking the "Continue Yes or No" question totally.

4. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

Variables only change if you assign them a new value. It has no concept of the user typing "yes" or "no" unless you tell it what to do with the input.

Regards.

5. Member
Join Date
Feb 2013
Posts
68
Rep Power
0

## Re: "Continue Yes or No" Problem

Wouldn't a yes turn play in true ('play = true') and redo the do-while? It terminates fine if the input is "no" because it sets 'play = false'.

Java Code:
```Scanner in = new Scanner(System.in);
System.out.println("Would you like to continue (yes/no)?");

playAgain = in.nextLine();
if(playAgain.equals("yes"))
play = true;

if(playAgain.equals("no"))
{
play = false;
System.out.println("Program is terminated");```

6. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

Ah, I see what you have done. I ignored the commented code assuming this was redundant. I presume you just commented out the bit that didn't work and the end of the while loop was originally below this code.

I'll have another look and get back to you.

Regards.

7. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

LetsGOBlue, can you confirm this code was previously within the do while loop.

There is also no need to redeclare the Scanner, you can declare this once at the start of the your program and just keep reusing this.
Regards.

8. Member
Join Date
Feb 2013
Posts
68
Rep Power
0

## Re: "Continue Yes or No" Problem

I put the "Continue Yes or No" code outside the do-while loop because the boolean play = false was outside as well.

9. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

This is the initial declaration of the 'play' variable. Any code you want to loop through should be contained in some form of a loop.

Regards.

10. Member
Join Date
Feb 2013
Posts
68
Rep Power
0

## Re: "Continue Yes or No" Problem

I can't believe I missed that...

Thank you.

11. Senior Member
Join Date
Oct 2010
Posts
393
Rep Power
7

## Re: "Continue Yes or No" Problem

You're welcome .

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•