自然对数e及e的x次方的计算(Calculation of natural exponent e and x power of e)

时间:2022-05-03 15:27:11

 看到8年前发的帖子活的好好的,感觉很好!

小修小补一下,再补上两张程序运行的截屏,提交自然对数e及e的x次方的计算(Calculation of natural exponent e and x power of e)

说明:关于自然对数e的计算,建议参考本人的最新帖子,

 12以内阶乘、自然对数e及e的x次方的计算(Factorial)(http://blog.csdn.net/hpdlzu80100/article/details/51721687),因为其论述更为严谨一些。


//Calculate natural exponent e and x power of e
//used for Java learning practice
//Modified by Pandeng Huang on 2016-06-14, pandenghuang@163.com
import javax.swing.JOptionPane;
public class Exponent_e {
    static double exponent=1.0;
    static double exponentx=1.0;
    
    public static double Factorial (int argument)
    {
    double factorial=1;
    if (argument<0)
    JOptionPane.showMessageDialog(null, "Please input a nonegative integer",
            "Exponent e Calculation",JOptionPane.ERROR_MESSAGE);
    else {
      if (argument==0)
      factorial=1;
      else if (argument>0) 
      {
        factorial=1;
         for (int i=1;i<argument+1;i++)
        factorial*=i;
      }
         }
    return factorial;
     }
    
    public static int Productx(int base, int argument)
    {
        int productx=1;
        for (int i=1;i<argument+1;i++)
            productx*=base;
        return productx;
    }
    
public static void main(String args[])
{
    String N,Base;
    int n;
    int base;
    N=JOptionPane.showInputDialog("Enter an integer N:");
    n=Integer.parseInt(N);
    Base=JOptionPane.showInputDialog("Enter an integer X:");
    base=Integer.parseInt(Base);
    
    if (n<0)
        JOptionPane.showMessageDialog(null, "Please input a nonegative integer",
                "Expoent e Calculation",JOptionPane.ERROR_MESSAGE);
    else
        {for (int i=1;i<n+1;i++)
        {exponent+=1.0/Factorial(i);
    exponentx+=Productx(base,i)/Factorial(i);}
    
    JOptionPane.showMessageDialog(null, "Calculated exponent e and x power of e are:"+exponent+
            " and "+exponentx,"Expoent e Calculation",JOptionPane.INFORMATION_MESSAGE);}
}
}


运行截屏:

 

自然对数e及e的x次方的计算(Calculation of natural exponent e and x power of e)

自然对数e及e的x次方的计算(Calculation of natural exponent e and x power of e)