Results 1 to 11 of 11
Thread: "Continue Yes or No" Problem
- 02-16-2013, 11:26 PM #1
Member
- Join Date
- Feb 2013
- Posts
- 60
- 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; } }
- 02-16-2013, 11:34 PM #2
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-16-2013, 11:41 PM #3
Member
- Join Date
- Feb 2013
- Posts
- 60
- 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.
- 02-16-2013, 11:46 PM #4
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-16-2013, 11:53 PM #5
Member
- Join Date
- Feb 2013
- Posts
- 60
- 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");
- 02-17-2013, 12:04 AM #6
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-17-2013, 12:10 AM #7
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-17-2013, 12:16 AM #8
Member
- Join Date
- Feb 2013
- Posts
- 60
- 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.
- 02-17-2013, 12:20 AM #9
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
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.
- 02-17-2013, 12:24 AM #10
Member
- Join Date
- Feb 2013
- Posts
- 60
- Rep Power
- 0
Re: "Continue Yes or No" Problem
I can't believe I missed that...

Thank you.
- 02-17-2013, 12:27 AM #11
Senior Member
- Join Date
- Oct 2010
- Posts
- 317
- Rep Power
- 3
Similar Threads
-
access denied("java.net.SocketPermission" "127.0.0.1:1099" "connect,resolve")
By klspepper in forum New To JavaReplies: 0Last Post: 12-07-2012, 08:29 AM -
Convert string operation symbols "+", "-", "/", "*" etc.
By Googol in forum New To JavaReplies: 3Last Post: 10-30-2012, 03:06 PM -
Why do we need "continue" in this piece of code?
By tariqm in forum New To JavaReplies: 4Last Post: 04-08-2012, 05:47 PM -
problem with argument list and precedence "(" and ")"
By helpisontheway in forum Advanced JavaReplies: 6Last Post: 12-24-2009, 07:50 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
.

Bookmarks