[英]Where should I use command-line arguments instead of Scanner?
I know that when you create your main
method of a Class
in Java
you create it as follows: 我知道,当您使用Java
创建Class
main
方法时,将按以下方式创建它:
public static void main(String[] args)
I also know that the array
of Strings
named args
it's used to enter command-line parameters in the main
method but I develop my applications on Eclipse
and I never had used it because I always have entered my values by Scanner
. 我还知道,名为args
的Strings
array
用于在main
方法中输入命令行参数,但是我在Eclipse
上开发应用程序,但我从未使用过它,因为我总是通过Scanner
输入值。
It is a bad practise to use Scanner
instead of command-line parameters? 使用Scanner
而不是命令行参数是一种不好的做法? Has args
array another purpose? args
数组还有其他用途吗? When should I use args
array? 什么时候应该使用args
数组?
Thanks in advance! 提前致谢!
In reverse order: 以相反的顺序:
When should I use
args
array? 什么时候应该使用args
数组?
When you are passing command-line parameters. 传递命令行参数时。
Has
args
array another purpose?args
数组还有其他用途吗?
No. It's for passing command-line parameters. 否。用于传递命令行参数。
Is it a bad practise to use
Scanner
instead of command-line parameters? 使用Scanner
而不是命令行参数是不好的做法吗?
It's not always possible to use a Scanner
. 并非总是可以使用Scanner
。 A Scanner
might read from STDIN, but in many UNIX systems it is prefered to read command input from STDIN and use command line arguments for program options (eg --help
or -h
). Scanner
可能会从STDIN中读取,但是在许多UNIX系统中,它倾向于从STDIN中读取命令输入,并使用命令行参数作为程序选项 (例如--help
或-h
)。
Scanner
and the parameter for main
are two completely different things. Scanner
和main
参数是两个完全不同的东西。 A scanner can be used to ask the user for some kind of action. 扫描仪可用于询问用户某种操作。 The parameters for main
are relevant when you want to launch the jar either from another application or via commandline. 当您要从另一个应用程序或通过命令行启动jar时, main
的参数是相关的。 Cooked down: whether to use Scanner
or parameters is rather a question of the design of the software, not coding-style. 简而言之:是否使用Scanner
或参数只是软件设计的问题,而不是编码样式的问题。
It entirely depends on your circumstances. 这完全取决于您的情况。 Ask yourself the question: "Where are the values coming from? What is the best way for my program to receive these values?" 问自己一个问题:“这些值从哪里来?我的程序接收这些值的最佳方法是什么?”
If the answer is "a user", then prompting the user, thereby guiding the user to the values needed, is likely the best answer. 如果答案是“用户”,则提示用户,从而将用户引导至所需的值,可能是最佳答案。
Any other answer means the values are likely coming from some automated process, which means that "prompting" is a bad choice. 任何其他答案都意味着值可能来自某个自动化过程,这意味着“提示”是一个错误的选择。 Now there are suddenly a lot of choices, again controlled by your circumstances: 现在突然有很多选择,再次由您的情况决定:
Sometimes a combination is appropriate, eg a command-line argument to find the configuration file , which may include a directory to scan for data files . 有时,适当的组合是合适的,例如,使用命令行参数来查找配置文件 ,其中可能包括用于扫描数据文件的目录 。
You can even do both "prompting" and command-line arguments. 您甚至可以同时执行“提示”和命令行参数。 If all needed values are supplied as arguments, use them, otherwise prompt for the missing values. 如果将所有需要的值作为参数提供,请使用它们,否则提示输入缺少的值。 Providing no arguments would then prompt for all values. 不提供任何参数将提示输入所有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.