Results 1 to 11 of 11
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default "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. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default 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. #3
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default 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. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default 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. #5
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default 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. #6
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default 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. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default 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. #8
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default 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. #9
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default 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. #10
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: "Continue Yes or No" Problem

    I can't believe I missed that...

    Thank you.

  11. #11
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  3. Why do we need "continue" in this piece of code?
    By tariqm in forum New To Java
    Replies: 4
    Last Post: 04-08-2012, 05:47 PM
  4. problem with argument list and precedence "(" and ")"
    By helpisontheway in forum Advanced Java
    Replies: 6
    Last Post: 12-24-2009, 07:50 AM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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