Friday, May 20, 2005

黃金比例

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

/**
Computes the amount of green crud after a given number of days and a
given starting amount.
*/
public class GreenCrud {

public static void main(String[] args) throws IOException {
// Make a BufferedReader to read data from the console
BufferedReader console = new BufferedReader(
new InputStreamReader(System.in));

// Read in the initial size of the crud population
System.out.println(
"Enter initial green crud population size (in pounds), ");
System.out.println("or a blank line to quit:");
String size = console.readLine();

// Stop on blank line of EOF (e.g., if the user presses Ctrl-D)
while ((size != null) && (size.length() > 0)) {
double initialSize = Double.parseDouble(size);

// Read in the number of days
System.out.println("Enter the number of (whole) days:");
int days = Integer.parseInt(console.readLine());


// --------------------------------
// ----- ENTER YOUR CODE HERE -----
// --------------------------------
double f0=1 ,f1=1 ,third=0;
for (int i=2;i<=100;i++)
{third=f0+f1;
f0=f1;
f1=third ;
System.out.println("f"+i+"="+f1+","+"ratio="+f1/f0);
}




// --------------------------------
// --------- END USER CODE --------
// --------------------------------


// Start again, giving the user a chance to cancel by entering
// a blank line.
System.out.println();
System.out.println(
"Enter initial green crud population size (in pounds), ");
System.out.println("or a blank line to quit:");
size = console.readLine();
}

}

}

0 Comments:

Post a Comment

<< Home