创建类对象以执行阶乘递归



我被分配了一个使用递归编写程序的问题,该程序工作正常,我唯一的问题是指定创建一个类对象来执行阶乘计算而不是方法...我这样做对吗?

import java.util.*;
import java.io.*;
public class Assignment2A {
    public static void main(String[] args){ 
        Scanner scan = new Scanner(System.in); //sets up scanner
        boolean correct = true;             //sets up boolean operation
        while(true){
            try{                                //try catch operation
                System.out.println("Welcome to the factorial function");
                System.out.println("Please enter a number or press 0 to exit");
                long startTime =System.nanoTime();
                System.out.println("Start Time: "+startTime+" nanoseconds."); //display the start time
                int n = scan.nextInt();             //scans the input
                int factorial= fact(n);             
                System.out.println("The Factorial of the number entered is: " + factorial);
                long taskTime = System.nanoTime() - startTime;
                System.out.println("Task Time: "+taskTime+" nanoseconds.nn");
            }catch(Exception e){                //checks if the input is a string or character
                System.out.println("That is not a number!");    //displays that the input is invalid
                long startTime =System.nanoTime();
                long taskTime = System.nanoTime() - startTime;
                System.out.println("Task Time: "+taskTime+" nanoseconds.");
                System.exit(0);         //exits the program
            }
        }
    }
    static int fact(int n){
        int output;                 //sets the output as int
        long startTime = System.nanoTime();     //gets the start time of the cpu
        long taskTime = System.nanoTime()-startTime;    //gets the task time
        if (n==0){                      //if the input is 0, system exits
            System.out.println("Exiting Program");      
            System.out.println("Task Time: "+taskTime+ " nanoseconds."); //displays cpu time
            System.exit(0);
        }
        else if(n==1){
            return 1;           //if input is 1, the factorial of 1 is 1
        }
        output = fact(n-1)*n;   //recursive method for factorial
        return output;
    }
}

不,你创建了一个计算阶乘的方法 fact(int n)。

您应该使用计算阶乘的方法创建一个新类。将实例化类的对象,然后从类对象调用计算阶乘的方法。

例:

// outside the while loop instantiate your class object for
// dealing with factorial computations
Factorial factorial = new Factorial();
// inside the while loop, instantiate your method for 
// computing factorial of "n", invoking your factorial 
// computation method that is defined inside your class
int result = factorial.computeFactorial(n);

如果要创建一个类对象,请从方法中删除静态此外,如果您使用动态编程方法来解决此问题会更好。

import java.util.*;
import java.io.*;
public class Assignment2A {
    public static void main(String[] args){
        Assignment2A x = new Assignment2A();
        x.askForOutput();
    }
    int fact(int n){
        int output;                 //sets the output as int
        long startTime = System.nanoTime();     //gets the start time of the cpu
        long taskTime = System.nanoTime()-startTime;    //gets the task time
        if (n==0){                      //if the input is 0, system exits
            System.out.println("Exiting Program");      
            System.out.println("Task Time: "+taskTime+ " nanoseconds."); //displays cpu time
            System.exit(0);
        }
        else if(n==1){
            return 1;           //if input is 1, the factorial of 1 is 1
        }
        output = fact(n-1)*n;   //recursive method for factorial
        return output;
    }
    public void askForOutput(){
        Scanner scan = new Scanner(System.in); //sets up scanner
        boolean correct = true;             //sets up boolean operation
        while(true){
            try{                                //try catch operation
                System.out.println("Welcome to the factorial function");
                System.out.println("Please enter a number or press 0 to exit");
                long startTime =System.nanoTime();
                System.out.println("Start Time: "+startTime+" nanoseconds."); //display the start time
                int n = scan.nextInt();             //scans the input
                int factorial= fact(n);             
                System.out.println("The Factorial of the number entered is: " + factorial);
                long taskTime = System.nanoTime() - startTime;
                System.out.println("Task Time: "+taskTime+" nanoseconds.nn");
            }catch(Exception e){                //checks if the input is a string or character
                System.out.println("That is not a number!");    //displays that the input is invalid
                long startTime =System.nanoTime();
                long taskTime = System.nanoTime() - startTime;
                System.out.println("Task Time: "+taskTime+" nanoseconds.");
                System.exit(0);         //exits the program
            }
        }
    }

}

如果您希望将其放在单独的类中,只需将 fact() 方法封装在新类中即可。 但是请注意,所花费时间的打印输出不正确 - 它仅计算最终进入递归方法的时间。

public class Factorial {
   public static int compute( int val ) {
      long startTime = System.nanoTime();
      int result = fact( val );
      long taskTime = System.nanoTime()-startTime;
      System.out.println("Task Time: "+taskTime+ " nanoseconds.");
   }
   protected static int fact( int val ) {
      if (n==1){
         return val;
      }
      return fact(val-1)*val;
   }
}

然后在你主,

 int answer =  Factorial.compute(value);

最新更新