d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > "continue Yes Or No" Help!
Add Reply New Topic New Poll
Member
Posts: 9,274
Joined: May 4 2010
Gold: 0.01
Feb 16 2013 04:34pm
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.

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;

   
   }
}
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Feb 16 2013 04:59pm
you seem to have misunderstood how to write a while loop

do a lookup on the proper syntax for whlie loops and for do while loops

This post was edited by irimi on Feb 16 2013 05:00pm
Member
Posts: 9,274
Joined: May 4 2010
Gold: 0.01
Feb 16 2013 05:12pm
Quote (irimi @ Feb 16 2013 06:59pm)
you seem to have misunderstood how to write a while loop

do a lookup on the proper syntax for whlie loops and for do while loops


It works fine on all of my other codes?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Feb 16 2013 05:23pm
Quote (irimi @ Feb 16 2013 06:59pm)
you seem to have misunderstood how to write a while loop

do a lookup on the proper syntax for whlie loops and for do while loops


this.

if you are planning on this to loop until user answers no the entire code should be wrapped in a loop. should write down what you want your application to do on paper before attempting to write it in code.

This post was edited by AbDuCt on Feb 16 2013 05:23pm
Member
Posts: 9,274
Joined: May 4 2010
Gold: 0.01
Feb 16 2013 05:31pm
I fixed it thanks guys
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll