Results 1 to 6 of 6
Thread: Recusion problem
- 12-15-2008, 08:31 AM #1
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
Recusion problem
Hi Everyone..
I' am a new member of this site. I have a problem with my program. We use recursion in creating a program called fibonacci. Our instructor give us this program last December 12, 2008. He wanted us to solve the lowest number and the higher number and display the answer and it will add the previous number. We use java programming language. The output will be like this:
11235813....
We use recursion in creating this program and I dont know how to start it.
Your help is well appreciated. Thank You.
- 12-16-2008, 07:12 AM #2
Member
- Join Date
- Dec 2008
- Posts
- 2
- Rep Power
- 0
try this
public class Fibonacci {
private long number;
private int index;
public Fibonacci ( ) {
number = 0;
index = 0;
}
public static void main ( String [] args ) {
Fibonacci program = new Fibonacci ( );
program.GetIndex ( );
program.FindAndPrint ( );
}
public void GetIndex ( ) {
TextReader keyboard = new TextReader ( );
do {
System.out.print ( "\n\n\nEnter a Fibonacci index (1 to 25) -> " );
index = keyboard.readlnInt ( );
} while ( index < 1 || index > 25 );
}
public void FindAndPrint ( ) {
number = FindFib ( index );
System.out.println ( "The Fibonacci number at index " + index + " is: " + number );
}
public int FindFib ( int i ) {
// System.out.println ( i );
if ( i == 1 || i == 2 )
return 1;
else
return ( FindFib (i - 2) + FindFib(i - 1) );
}
}
- 12-16-2008, 12:58 PM #3
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
hi
hi,
Thank you for the program but it has 2 errors. line 17. It is said that cannot find symbol
symbol : class TextReader
location: class Fibonacci
TextReader keyboard = new TextReader ( );
- 12-16-2008, 01:07 PM #4
Member
- Join Date
- Dec 2008
- Location
- Davao Oriental
- Posts
- 29
- Rep Power
- 0
hi,
Thank you for the program but it has 2 errors. line 17. It is said that cannot find symbol
symbol : class TextReader
location: class Fibonacci
TextReader keyboard = new TextReader ( );
- 12-16-2008, 02:52 PM #5
@ptsofathrty: It's probably not a good idea to post complete code for the OPs. Too much temptation to cut & paste & forget (no learning involved). The purpose of this forum is for people to learn, not get free code. I'm pretty sure that Mika doesn't know what the code does because he/she hasn't figured out what the TextReader method is supposed to do or why it's failing.
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-23-2009, 02:04 PM #6
Member
- Join Date
- Jan 2009
- Posts
- 3
- Rep Power
- 0
hello...... i have another problem, how could i display messages or statement in a JOptionPane which this JOptionPane was inside of a LOOP.... someone suggested to use ConfirmationDialog isntead of MessageDialog.... but my INSTRUCTOR said that I must use Message dialog because I`m only displaying the result.
ex. in BufferedReader the output is like this: 1 2 3 4 5, how could I display this in JoptionPane in the same way?
code:
"only the loop part"
[1] for(int countr=0; countr<=20; countr++){
System.out.print(" "+countr);
}
in this way there is no problem it will print 0 1 2 to 20;
[2]for(int countr=0; countr<=20; countr++){
JOptionPane.showMessageDialog(" "+countr);
}
in this way u need to click the OK before you can view the other numbers so you need to click OK 20 times;
HOW COULD I DISPLAY IT IN ONE JOPTIONPANE DIALOG THE 21 NUMBERS?
Similar Threads
-
Recusion Help
By calicocal in forum New To JavaReplies: 3Last Post: 12-02-2008, 08:18 AM


LinkBack URL
About LinkBacks

Bookmarks