1 2 Previous Next 24 Replies Latest reply on Jun 6, 2008 12:11 AM by 3004

# Help Again!!  Recursive Loop??

I am trying to restart this class from the beginning after it runs. Can anyone help?
import java.util.*;

public class Fibonacci_Sequence
{

/**This class displays the Fibonnaci Sequence based on the number of returns in the
*sequence you would like. So if you enter 8 you would get: 1, 1, 2, 3, 5, 8, 13, 21, 34.
*After the class runs, you have the option to run the sequence again.
*/

public static void main(String[] args)

{
int input = 0, fib1 = 0, fib2 = 1, fib3;

Scanner keyboard = new Scanner(System.in);

System.out.println("Please indicate how many numbers in the " +
"Fibonacci Sequence (between 1 and 46) you would like to display: ");

input = keyboard.nextInt();

while (input >= 1)
{System.out.println (fib1 + fib2);
input--;
fib3 = fib1 + fib2;
fib2 = fib1;
fib1 = fib3;
}

System.out.println ("Would you like to run another instance of the Fibonnaci" +
"Sequence? Enter yes or no: ");

//I need the command that will return me to the beginning of the class!!
//thought that entering **Fibonaci_Sequence()** would return it to the
//beginning, but it doesn't work.

else System.out.println("Goodbye!");
System.exit(0);

}

}

}
• ###### 1. Re: Help Again!!  Recursive Loop??
So, you want to do everything in main more than once.

I see from your code that you know how to do a given thing more than once, so what's the problem?
• ###### 2. Re: Help Again!!  Recursive Loop??
Exactly..I need it to run the **Fibonnaci_Sequence** class again, but I don't know the command line to run it again. I'm sorry if I'm not explaining it well enough, I'm in my 2nd week of CSC.
• ###### 3. Re: Help Again!!  Recursive Loop??
jverd wrote:
So, you want to do everything in main more than once.

I see from your code that you know how to do a given thing more than once, so what's the problem?
hahaa. nice one!
just to elaborate on that, use a loop.
• ###### 4. Re: Help Again!!  Recursive Loop??
Thanks...I know I need to run a loop...but I DONT KNOW WHAT I SHOULD PUT IN after the if statement.
• ###### 5. Re: Help Again!!  Recursive Loop??
Instead of writing a program that is a single, too-long main method, break your problem up into smaller methods. Calling a method repeatedly should be easy to conceptualize.
• ###### 6. Re: Help Again!!  Recursive Loop??
I would excpet I have no idea what you mean by that. Imagine you are dealing with a child that asks you to complete a sentence for them. Can anyone complete my sentence that will loop the class back to the beginning?
• ###### 7. Re: Help Again!!  Recursive Loop??
``````while (condition) {
...
subroutine(...);
...
}``````
• ###### 8. Re: Help Again!!  Recursive Loop??
Once again I have no clue what that means. I swear I'm not trying to be difficult, this is my first program I've ever written on my own. If you read through the code, I get stuck at this point

//I need the command that will return me to the beginning of the class!!
//thought that entering **Fibonaci_Sequence()** would return it to the
//beginning, but it doesn't work.

else System.out.println("Goodbye!");
System.exit(0);

I just need to know what to put in where the // starts. Can someone just fill it in?
• ###### 9. Re: Help Again!!  Recursive Loop??
koinz04 wrote:
Once again I have no clue what that means.
You've already used while to do something more than once. What part of that sample while did you not get? Do you not know how to write and call methods?

>

//I need the command that will return me to the beginning of the class!!
//thought that entering **Fibonaci_Sequence()** would return it to the
//beginning, but it doesn't work.
Why are you trying to use an if rather than a loop structure (such as while) to do something more than once?
I just need to know what to put in where the // starts. Can someone just fill it in?
There is nothing you can put there that will make it go back to the beginning. Stop thinking in terms of if.
• ###### 10. Re: Help Again!!  Recursive Loop??
go back a step and make a pseudocode for your program, then think about how to go about doing what u want to do. try that and u will be surprised how easy it is.
• ###### 11. Re: Help Again!!  Recursive Loop??
I cannot figure this out. Is this the proper way to call the class back to the begining?

do {
Fibonacci_Sequence();

else System.out.println("Goodbye!");
System.exit(0);
• ###### 12. Re: Help Again!!  Recursive Loop??
It's a start.

When you went through [this section of the tutorial|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/while.html] slowly, carefully, and throughly, did you notice any elses?

Also, thnk carefully about exactly which steps you need to do multiple tmes.
• ###### 13. Re: Help Again!!  Recursive Loop??
So you're saying that by removing else it will continue to loop through as long as the answer is yes? Once it becomes something else, it will move to the next line...which will exit the program.

do {
Fibonacci_Sequence();

System.exit(0);
• ###### 14. Re: Help Again!!  Recursive Loop??
koinz04 wrote:
So you're saying that by removing else it will continue to loop through as long as the answer is yes? Once it becomes something else, it will move to the next line...which will exit the program.

do {
Fibonacci_Sequence();