简体   繁体   中英

Java command line parameters

I'm just wondering how to do Java command-line with parameters? For example :

javac Test.java

then

java Test 3 5

then after running the command line

expected output:

3 5

public class Test{

    public static void main(String[] args){

      int num1 = 1;
      int num2 = 2;

      System.out.println(num1,num2);


     }
    }

I think you can use args as input parameters.

public static void main(String[] args){
    System.out.println(args[0] + " " + args[1]);
}

Fix for your code is

public class Test {

    public static void main(String[] args) {
        if (args.length > 1) {
            int num1 = args[0];
            int num2 = args[1];
            System.out.println(num1 + "-" + num2);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM