首页 > 解决方案 > 使用 String[] args 传递参数

问题描述

我强烈怀疑我在这里很愚蠢,但我以前从未见过任何人这样做。在本课程作业中,他们使用 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 包含某些内容?

标签: javaparameter-passing

解决方案


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

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

所以当你调用程序喜欢myprogram.exe gate_bottommyprogram.exe gate_top条件将被触发


推荐阅读