horsemanship世界

星期一, 3月 27, 2006

Lab6

單迴圈版

public class Untitled7 {
public Untitled7() { }
public static void main(String[] args) throws IOException {
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("輸入一個數字 X");
String w = keyboard.readLine();
double x = Integer.parseInt(w);
double y=1,z=1,q=1;
for(int i=1;i<=10;i++) { y=y*i; //階乘 z=x*z; //次方 q=q+z/y; } System.out.println("exp(
X)="+q);}}

結果:
輸入一個數字 X
10
exp(
X)=12842.305114638448

================================================================
雙迴圈版

public class Untitled7 {
public Untitled7() {
}
public static void main(String[] args) throws IOException {
BufferedReader keyboard= new BufferedReader(new InputStreamReader(System.in));
System.out.println("輸入一個數字X");
String x = keyboard.readLine();

int y = Integer.parseInt(x);
double m=1,n,w=1,q=1;
double z=1;

for(int i=1;i<=10;i++) { w=w*y; //x的次方迴圈 m=1; for(int j=1;j<=i;j++) { m=j*m; //階乘 } z=z+w/m; } System.out.println("
exp(X)="+z);}}

結果:

輸入一個數字X
3
exp(X)=20.079665178571425

0 Comments:

張貼留言

<< Home