//Calculate natural exponent e and x power of e
//used for Java learning practice
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",
"Expoent 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);}
}
}
发表于 @ 2008年04月08日 11:51:00|评论(loading...)|编辑