I, entitled
Write a class Computer, class contains a method to find the factorial of n.
The classes are packaged, and Java files App.java another packet introduced packets, Computer definition object classes in the main category,
Calling a method seeking the factorial of n (n is determined by the parameter), and outputs the result.
Second, the code segment
(1) Computer-based
CN Package; public class Computer { // Write a factorial public static int FAC (int NUM) { int Result = 1; IF (NUM == 1) { return Result; // if 1; return result directly } else { for (int I =. 1; I <= NUM; I ++) { result * = I ; // factorial } return result ; // return result } } }
(2) App class
Package the Test; Import java.util.Scanner; Import cn.Computer; // the computer incorporated App class in public class App { / ** * @param args * / public static void main (String [] args) { // Create computer objects Computer COM = new new Computer (); Scanner Reader = new new Scanner (the System.in); // Create a class method Scanner System.out.println ( "enter a number" ); int m = reader.nextInt (); // the m read from the keyboard System.out.println (m + "is the factorial" + com.fac (m));
Third, the results