使用String [] args传递参数

时间:2019-09-11 18:28:31

标签: java parameter-passing

我很怀疑我在这里很愚蠢,但我从未见过有人这样做过。在本课程中,他们不是在声明变量,而是使用main中声明的字符串数组参数。

static public void main(String[] args) {

    RandomAccessFile admin;


    byte init[] = {0};

    if (args.length != 1)
      System.err.println("usage: java {gate_bottom,gate_top}");

我的说明是 “为此,您需要指定一个参数。该参数应该是gate_bottom或gate_top”

我应该在哪里指定参数,以便args包含某些内容?

1 个答案:

答案 0 :(得分:1)

if(args[0].equals("gate_bottom"))

else if(args[0].equals("gate_top")) 

因此,当您调用myprogram.exe gate_bottommyprogram.exe gate_top之类的程序时,条件将被触发