中国开发网: 论坛: 程序员情感CBD: 贴子 229984
nostalgic: 测CPU性能呀
import java.awt.*;
import java.awt.event.*;

// Java extension packages
import javax.swing.*;

public class FibonacciTest extends JApplet
implements ActionListener {

JLabel numberLabel, resultLabel;
JTextField numberField, resultField;

// set up applet GUI
public void init()
{
// obtain content pane and set its layout to FlowLayout
Container container = getContentPane();
container.setLayout( new FlowLayout() );

// create numberLabel and attach it to content pane
numberLabel =
new JLabel( "Enter an integer and press Enter" );
container.add( numberLabel );

// create numberField and attach it to content pane
numberField = new JTextField( 10 );
container.add( numberField );

// register this applet as numberField ActionListener
numberField.addActionListener( this );

// create resultLabel and attach it to content pane
resultLabel = new JLabel( "Fibonacci value is" );
container.add( resultLabel );

// create numberField, make it uneditable
// and attach it to content pane
resultField = new JTextField( 15 );
resultField.setEditable( false );
container.add( resultField );

} // end method init

// obtain user input and call method fibonacci
public void actionPerformed( ActionEvent e )
{
long number, fibonacciValue;

// obtain user input and conver to long
number = Long.parseLong( numberField.getText() );

showStatus( "Calculating ..." );

// calculate fibonacci value for number user input
fibonacciValue = fibonacci( number );

// indicate processing complete and display result
showStatus( "Done." );
resultField.setText( Long.toString( fibonacciValue ) );

} // end method actionPerformed

// Recursive definition of method fibonacci
public long fibonacci( long n )
{
// base case
if ( n == 0 || n == 1 )
return n;

// recursive step
else
return fibonacci( n - 1 ) + fibonacci( n - 2 );

} // end method fibonacci

} // end class FibonacciTest

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录