我被分配了一个问题,使用递归编写程序,这是正常工作,我唯一的问题是它被指定创建一个类对象来进行阶乘计算而不是方法...我这样做了吗?
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.\n\n");
}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;
}
}
答案 0 :(得分:3)
不,你创建了一个计算阶乘的方法,事实(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);
答案 1 :(得分:1)
如果要创建类对象,请从方法中删除静态 如果你使用动态编程方法解决这个问题会更好。
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.\n\n");
}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
}
}
}
}
答案 2 :(得分:1)
如果您希望将它放在一个单独的类中,只需将您的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);