0
import java.util.Scanner;

public class TaylorSin {

    /* I am trying to approximate sin(x) using Maclaurin Series...
    I am trying to get 1 term (1 Step Towards my approximation) the exponent divided by a factorial...
    However I get stuck in the calculateExponent method...
    Thank You in Advance! */

    public static void main(String[] args) {

        Scanner Keyboard = new Scanner(System.in);

        double base; int exponent, n;

        System.out.println();
        System.out.println("Enter Base: ");
        base = Keyboard.nextDouble();
        System.out.println("Enter Exponent: ");
        exponent = Keyboard.nextInt();
        System.out.println("Enter n: ");
        n = Keyboard.nextInt();
        System.out.println();

        System.out.println(calculateTerm(2.0, 4, 2));
    }       

    public static int calculateFactorial(int n) {

        int facto = 1;

        for (int i = 1; i <= n; i++)

        {facto = facto * i;}

        return facto;

    }

    public static double calculateExponent(double base, int exponent) {

        if(exponent == 0){return 1;}

        else { return base * exponent(base, exponent - 1);} //The mistake occurs on that line...    
    }       

    public static double calculateTerm(double base, int exponent, int n) {

        double term = (calculateExponent(base, exponent)/calculateFactorial(n));

        return term;
    }
}                   
4

2 回答 2

2

代替

else { return base * exponent(base, exponent - 1);} //The mistake occurs on that line...    

你的意思

else { return base * calculateExponent(base, exponent - 1);} //The mistake occurs on that line... 

(即替换exponentcalculateExponent

方法名称exponent未定义。这就是错误消息试图告诉您的内容。

于 2013-02-10T06:57:14.050 回答
0

你是说 Math.pow 吗?数学.exp?

http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Math.html

于 2013-02-10T06:31:22.880 回答