static, this, package - factorial of n -

First, the program title

Write a class Computer, class contains a method to find the factorial of n. The class package, and another package is introduced in the package file App.java German Java, the class definition of the object in the main Computer class, calling methods factorial of n - (n is determined by the parameter), and outputs the result .

Second, the operating results

 

 

Third, code analysis

1.Computer

WWW Package; 

public  class Computer {                 // create a Computer-based 
    
    public  static  int Jiecheng ( int n-) {            // Create method Jiecheng 
        int Result = . 1 ;                              // factorial calculation 
        for ( int I = . 1 ; I <= n-; I ++ ) { 
            Result * = I; 
        } 
        
        return Result; 
    } 

}

2.App

package com;

import java.util.Scanner;

import www.Computer;                             //引入www包下的Computer类
public class App {

    public static void main(String[] args) {
        Scanner reader=new Scanner(System.in);  //创建Scanner对象
        System.out.println("输入");
        Computer c=new Computer();               //创建对象
        int a=reader.nextInt();                   //从键盘输入
        
        
        System.out.println(a+"的阶乘为:"+c.jiecheng(a));     //输出:调用成员变量c下方法
    }

}

 

Guess you like

Origin www.cnblogs.com/wanghainan/p/11541560.html