階乗計算、オイラー数、オイラーパワーxの計算は、以下の「階乗」クラスと「behzat」クラスを用意しました。プログラムを実行すると、メソッドごとに同じ入力番号が返されます。誰かが問題が何であるかをアドバイスできますか?あなたの親切な援助に感謝します。
これは階乗クラスです:
public class factorial {
public void instructions()
{
System.out.printf("Enter your choice:\n",
" 1 to calculate a factorial value of an integer.\n",
" 2 to calculate mathematical constant e.\n",
" 3 to calculate e^x.\n",
" 4 to end.\n");
} // End of instructions
// ******* a ********
public double calculateInputValue (int n){
int ans=1;
for (int i = 1; i <= n; i++){
ans = ans * i;
}
return ans;
} // end method calculateInputValue
// ****** b *******
public double calculate_e_value(double terms)
{
double e = 1;
for(int i = 1; i < terms; i++)
e += ((double) 1 /(calculateInputValue(i) ));
return e;
}
//********* c ***********
public double compute_ex(double terms) // method for e power x
{
int x = 1;
double e = 1;
for( int i = 1; i < terms; i++)
e += (Math.pow(x,i)/(calculateInputValue(i) ));
x++;
return e;
}
}
およびメインを含むbehzatクラス:
import java.util.Scanner; // Program uses scanner.
public class behzat
{
private static Scanner input;
public static void main(String[] args)
{
factorial myfactorial = new factorial();
myfactorial.instructions();
System.out.printf(" ? ");
int choice;
input = new Scanner(System.in);
choice = input.nextInt();
switch(choice)
{
case 1 :
System.out.printf("enter number for factorial calculation");
int number = 1;
number = input.nextInt();
myfactorial.calculateInputValue(number);
// If integer greater or 0, print "factorial is equal to: %d"
if ( number >= 0 )
System.out.printf("Factorial is equal to:%d\n", number);
// otherwise, print "Input value not valid."
else
System.out.printf( "Error: Input value not valid.\n");
break;
case 2:
System.out.printf("enter number of terms to calculate of e constant.\n");
number = input.nextInt();
myfactorial.calculate_e_value(number);
System.out.printf(" e value is %2d\n", number);
break;
case 3:
System.out.printf("enter power x value of e constant.\n");
number = input.nextInt();
myfactorial.compute_ex(number);
System.out.printf(" e power x is %2d\n", number);
break;
default:
myfactorial.instructions();
}
}
}