gpt4 book ai didi

java - 命令行参数显示 0?

转载 作者:行者123 更新时间:2023-11-30 08:04:00 25 4
gpt4 key购买 nike

package commandLine;

public class commandLine {

public static void main(String[] args) {
System.out.println("There are " +args.length+ " Command-line Arguments");
System.out.println("They are: ");
for(int i=0;i<args.length;i++){
System.out.println("arg["+i+"]: "+args[i]);
}

}

}

我想检查我的命令行参数的长度并遍历它们以显示命令行数组。但是,它说我的命令行参数是 0?怎么会这样?

最佳答案

官方Java tutorial关于命令行参数。

Command-Line Arguments

A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched.

The user enters command-line arguments when invoking the application and specifies them after the name of the class to be run. For example, suppose a Java application called Sort sorts lines in a file. To sort the data in a file named friends.txt, a user would enter:

java Sort friends.txt

When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".

Echoing Command-Line Arguments

The Echo example displays each of its command-line arguments on a line by itself:

public class Echo {
public static void main (String[] args) {
for (String s: args) {
System.out.println(s);
}
}
}

The following example shows how a user might run Echo. User input is in italics.

java Echo Drink Hot Java

Drink

Hot

Java

Note that the application displays each word — Drink, Hot, and Java — on a line by itself. This is because the space character separates command-line arguments. To have Drink, Hot, and Java interpreted as a single argument, the user would join them by enclosing them within quotation marks.

java Echo "Drink Hot Java" Drink Hot Java

如果您使用的是 IDE(Eclipse 等),则必须通过某种运行配置指定命令行参数。例如 Eclipse :

Eclipse

关于java - 命令行参数显示 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com