(1)doc环境下
cmd->cd java文件所在目录->javac ValueOfDemo.java->java ValueOfDemo parameter 98 26
说明:程序向main(String[] args)的args中传递两个参数98 26(用一个或多个空格隔开)给主程序。
public class ValueOfDemo {
public static void main(String[] args) {
//this program requires two arguments on the command line
if (args.length == 2) {
//convert strings to numbers
float a = (Float.valueOf(args[0]) ).floatValue();
float b = (Float.valueOf(args[1]) ).floatValue();
//do some arithmetic
System.out.println("a + b = " + (a + b) );
System.out.println("a - b = " + (a - b) );
System.out.println("a * b = " + (a * b) );
System.out.println("a / b = " + (a / b) );
System.out.println("a % b = " + (a % b) );
} else {
System.out.println("This program requires two command-line arguments.");
}
}
}
结果:
a + b = 124.0
a - b = 72.0
a * b = 2548.0
a / b = 3.7692308
a % b = 20.0
(2)myeclipse中命令行参数传递
菜单栏的Run -> Run Configurations 在Arguments的Program arguments里输入参数 一行一个