對於階乘計算,歐拉數和歐拉功率x計算,我在「階乘」類和「behzat」類下編寫。當我執行程序時;它會爲每個方法返回相同的輸入數字。任何人都可以建議問題是什麼。非常感謝您的協助。Java程序僅返回輸入,未在方法中顯示計算結果
這是階乘類:
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();
}
}
}
不是關於你的問題,但還是覺得應該說:這是常見的做法是讓班從大寫字母和函數/變量開始,在Java中使用小寫字母。 – 2013-03-26 21:06:34
非常感謝您的關注,Adrian。 – Behzat 2013-03-26 21:18:53