20194638-stasic this包总结--求阶乘

时间:2019-09-18
本文章向大家介绍20194638-stasic this包总结--求阶乘,主要包括20194638-stasic this包总结--求阶乘使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一.程序题目

编写一个类Computer,类中含有一个求n的阶乘的方法。将该类打包,并在另一包中Java文件App.Java中引入包,在主类中定义Computer类的对象,调用求n的阶乘的方法(n值由参数决定),并将结果输出。

二.运行测试

三.代码

package b;               

public class Computer {      //computer类
public int jiecheng(int n){   //定义方法求阶乘
	int j=1;
	for(int i=1;i<=n;i++){
		j*=i;}

return j;
}
}

  

package a;
import java.util.*;      
import b.*;                       //引入b包

public class Sqx {

	
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		System.out.println("请输入数为:");
		int n=reader.nextInt();                         //随机数输入
		Computer computer=new Computer();//创建对象
		System.out.println("阶乘为:"+computer.jiecheng(n));//使用方法求阶乘并输出
		

	}

}

  

原文地址:https://www.cnblogs.com/lllm/p/11541500.html