/*
* Main.java
*
* Created on 2009年3月23日, 下午 4:31
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package javaapplication13;
import java.util.Scanner;
/**
*
* @author KFN
*/
public class Main {
/** Creates a new instance of Main */
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int x, n, s;
System.out.println("Please input a value 'x'");
x = keyboard.nextInt();
if ( x < 0)
{
System.out.println( "x should > 0" );
}
System.out.println("Please input a value 'n'.");
n = keyboard.nextInt();
if ( n < 0 )
{
System.out.println( "n should > 0" );
}
do{
double ans = 0.0;
ans = tailor( x, n );
System.out.println("The answer of e^"+x+"="+ans);
System.out.println("Please input '0' to stop this program or other number to continue.");
s = keyboard.nextInt();
}while( s != 0 );
// TODO code application logic here
}
public static double tailor( int x, int n ) {
double sum = 0.0;
for( int i = 0 ; i <= n ; i++ ) {
System.out.println( "x^i:" + comp(x,i) ) ;
System.out.println( "n!:" + comp1(i) ) ;
sum = sum + comp( x, i )/comp1( i );
}
return sum;
}
public static double comp( int x , int n )
{
if ( n == 0 )
return 1.0;
double sum = 1.0;
for(int i = 0 ; i < n ; i++)
sum = sum * x;
return sum ;
}
public static double comp1( int n )
{
if(n == 0 )
return 1;
return n * comp1( n-1 );
}
}
沒有留言:
張貼留言