我想出了以下代码来计算给定数字的阶乘:
import java.lang.*;
import java.math.*;
import java.io.*;
import java.util.*;
@SuppressWarnings("unused")
class factorial_1{
public static void main(String args[]) throws IOException
{
System.out.println("enter a number: ");
String strPhone = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
strPhone = br.readLine();
BigInteger number = new BigInteger (strPhone);
BigInteger fact = new BigInteger("1");
BigInteger i = new BigInteger("1");
BigInteger step = new BigInteger("1");
final long start = System.currentTimeMillis();
final long durationInMilliseconds = System.currentTimeMillis()-start;
for ( ; i.compareTo(number) <= 0; i=i.add(step)){
fact = fact.multiply(i);
}
System.out.println("execute Long-Running Task took " + durationInMilliseconds + "ms.");
System.out.println("the factorial of "+ number +" is "+ fact);
}
}
如果您执行代码,它会从键盘读取一个数字,然后打印出它的阶乘
我将代码导出为 .jar 文件,并尝试从终端输入数字 (10) 作为输入
我按照这篇文章所说的那样做了如何使用命令行参数执行 jar,但是直到我再次输入数字之前什么都没有发生
- - - - - -终端 - - - - - -
roditis@NiSLab-pc2:~/Desktop$ java -jar import_number.jar 10
输入一个数字:
10
执行长时间运行的任务耗时 0 毫秒。
10 的阶乘是 3628800
- - - - - -终端 - - - - - -
我是 linux / 编程新手,我非常期待您的帮助
提前致谢
罗德蒂斯