0

对于阶乘计算,欧拉数和欧拉幂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();
        }

   }

}
4

3 回答 3

2

您应该检索变量中每个方法的返回值,然后打印它。例如:

int val = myfactorial.calculateInputValue(number);
if ( val >= 0 )                          
System.out.printf("Factorial is equal to:%d\n", val);

在您调用某个方法并打印检索到的值的任何地方都采用这种方式。那只会解决你的问题。

于 2013-03-26T21:05:54.630 回答
0

至少在您的阶乘结果打印语句中:

if ( number >= 0 )                          
        System.out.printf("Factorial is equal to:%d\n", number);

您正在打印输入number而不是阶乘的结果,这肯定总是打印您的输入number

您必须存储阶乘结果:

long res = myfactorial.calculateInputValue(number);
if ( number >= 0 )                          
        System.out.printf("Factorial is equal to:%d\n", res);

假设您的阶乘不会导致溢出。其他两种情况的类似错误。

于 2013-03-26T21:06:24.110 回答
0

我认为你应该让输入表达你的函数的结果:

例子:

number = input.nextInt();
number = 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);
于 2013-03-26T21:06:31.583 回答