一.程序题目
编写一个类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));//使用方法求阶乘并输出 } }