如何从终端执行带有参数的jar文件?
我想出了以下代码来计算给定数字的阶乘:如何从终端执行带有参数的jar文件?,jar,terminal,arguments,execute,Jar,Terminal,Arguments,Execute,我想出了以下代码来计算给定数字的阶乘: 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: ")
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);
}
}
import java.lang.*;
导入java.math.*;
导入java.io.*;
导入java.util.*;
@抑制警告(“未使用”)
类阶乘{
公共静态void main(字符串args[])引发IOException
{
System.out.println(“输入一个数字:”);
字符串strPhone=“”;
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
strPhone=br.readLine();
BigInteger编号=新的BigInteger(strPhone);
BigInteger事实=新的BigInteger(“1”);
BigInteger i=新的BigInteger(“1”);
BigInteger步骤=新的BigInteger(“1”);
最终长启动=System.currentTimeMillis();
最终长持续时间毫秒=System.currentTimeMillis()-开始;
对于(;i.compareTo(number)您应该尝试将值放在引号中,如roditis@NiSLab-pc2:~/Desktop$java-jar导入\u number.jar“10”
记住,main函数接受一个字符串对象数组,比如sopublicstaticvoidmain(String[]args)
编辑
很抱歉,我发现您根本没有读取args[]
参数。如果您向程序提供“10”
作为参数,则args[0]
将包含该值。请检查是否(args.length>=1)
在访问args
之前,您应该尝试将值放在引号中,如roditis@NiSLab-pc2:~/Desktop$java-jar导入\u number.jar“10”
记住,main函数接受一个字符串对象数组,比如sopublicstaticvoidmain(String[]args)
编辑
很抱歉,我发现您根本没有读取args[]
参数。如果您向程序提供“10”
作为参数,则args[0]
将包含该值。请检查是否(args.length>=1)
在您访问args
之前,非常感谢您的帮助:D从您的编辑中,我意识到我必须将BigInteger编号=新的BigInteger(strPhone);更改为BigInteger编号=新的BigInteger(args[0]);现在它像一个符咒一样工作:DMichiel非常感谢您的帮助:D从您的编辑中,我意识到我必须将biginger number=new biginger(strPhone);更改为biginger number=new biginger(args[0]);现在它像一个符咒一样工作:D