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... 

(つまり、exponentで置き換えcalculateExponentます)

メソッド名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 に答える