java命令行参数

(1)doc环境下

cmd->cd java文件所在目录->javac ValueOfDemo.java->java ValueOfDemo parameter  98 26

说明:程序向main(String[] args)的args中传递两个参数98  26(用一个或多个空格隔开)给主程序。

1. public class ValueOfDemo { 2. public static void main(String[] args) { 3. 4. //this program requires two arguments on the command line 5. if (args.length == 2) { 6. //convert strings to numbers 7. float a = (Float.valueOf(args[0]) ).floatValue(); 8. float b = (Float.valueOf(args[1]) ).floatValue(); 9. 10. //do some arithmetic 11. System.out.println("a + b = " + (a + b) ); 12. System.out.println("a - b = " + (a - b) ); 13. System.out.println("a * b = " + (a * b) ); 14. System.out.println("a / b = " + (a / b) ); 15. System.out.println("a % b = " + (a % b) ); 16. } else { 17. System.out.println("This program requires two command-line arguments."); 18. } 19. } 20. }

 

 

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里输入参数 一行一个

你可能感兴趣的:(java命令行参数)