简体   繁体   中英

How to write a java program that computes the value of e^x

I'm trying to figure out how to answer this question for my Java class, using only while loops:

Write an application that computes the value of mathematical constant e^x by using the following formula. Allow the user to enter the number of terms to calculate. e^x = 1 + (x/1.) + (x^2/2.) + (x^3/3.) + ...

I can't figure out how I would do this without also asking the user for a value for x? Below is the code that I created for calculating x with the number of terms and just the number 1 for the exponent of each fraction. Any help is appreciated

import java.util.Scanner;
public class FactorialB {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int counter = 0;
        float answer = 0;

        System.out.print("Enter number of terms: ");
    int n = scanner.nextInt();

    while (counter < n) {
            double factorial = 1;
            int factCounter = counter;
            while (factCounter > 1) {
        factorial = factCounter * factorial;
        factCounter--;
            }
            
            answer += 1 / factorial;
            counter++;
    }

    System.out.printf("e = %f%n", answer);
    }
}

Firstly the question you seem to be asking:

There is no way to make a program that will give e for a specific number unless you ask the user for that number.

However it might be that they just want you to make a method that provides the solution (if it were called) independently of user input. (because the code to get user input isn't very interesting, what is interesting is how you reach the result).

An alternative way to provide x and n are for instance passing them as commandline arguments. (args[] in your main would be a way to provide them)

I would create a separate method that receives x and n that covers the main calculation:

e^x = 1 + (x/1!) + (x^2/2!) + (x^3/3!) + ...

And separate methods that cover 'calculating a single term (x^1/1,), (x^2/2!), etc' and 'factorialize(n)'

public void calculatePartialE_term(int x, int n) {
    if (n == 0) {
        return 1; // this will allow you to use a while loop, covers the n = 0 case
    } else {
        // removed the implementation, but basically do 
        // x^n/n! here for whatever value of n this term is calculating.
    }
}

public int calcualteNFactorial(int n) {
    // assert n >= 1
    // use a while loop to calculate n factorial
}

the benefit of doing this in a separate methods is that you can prove / verify the working of calculatePartialE_term or calcualteNFactorial independently of one another.

now you can simply write a while loop based on x and n to do something like

public int calculateE_to_x(int x, int n) {
    int current = 0;
    int sum = 0;
    while (current <= n) {
        sum += calculatePartialE_term(x, current);
    }
}

I wouldn't expect your teacher to expect you to show code that handles user input but even if that is the case it will be easier for them to verify your work if the actual work (of calculating) is done in a separate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM