Java命令行参数。使用*作为乘法的参数 [英] Java command line arguments. Using * as an argument for multiplication

查看:124
本文介绍了Java命令行参数。使用*作为乘法的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行运行java计算器应用程序。参数如下:operator operand1 operand2。我可以为+和 - 成功运行java程序。

,例如

java calc + 2 4

java calc - 10 4

I am trying to run a java calculator application from command line. the parameters are as follows : operator operand1 operand2. I can successfully run the java program for + and - .
e.g.
java calc + 2 4
java calc - 10 4

但是当我尝试运行

java * 2 5

But when I try to run
java * 2 5

System.out.println(args [0]);

System.out.println(args [1]);

System.out.println(args [2]);

给出输出:

.classpath

.project

.settings

gives output:
.classpath
.project
.settings

我通过审判发现了使用单引号('*')的错误解决了我的问题。
我现在有两个问题。

1.使用单引号是正确的方法吗? (java calc'*'2 5)

2.在java命令行中*的含义是什么? (我试图在互联网上找到这个,但没有找到太多帮助)

I found out by trial and error that using single quotes( '*' ) solved my problem. SO i have two questions now.
1. Is using single quotes the right way to do it? (java calc '*' 2 5 )
2. What is the meaning of * in the java command line? (I've tried to find this on internet but didn't find much help)

谢谢,
Punit

Thanks, Punit

推荐答案

这不是Java,它是shell( cmd ,如果你在Windows上)你正在使用那个解释 * as当前目录中的所有文件和文件夹。

It's not Java, it's the shell (cmd if you're on Windows) you are using that interprets * as "all files and folders in the current directory".

所以当你写:

java calc * 2 5

您实际上会为您的程序提供以下参数:

You will actually give your program the following arguments:

java calc file_1 file_2 ... file_n 2 5

其中 file_1 ... file_n 是所有文件(和当前目录中的文件夹)。

Where file_1 ... file_n are all files (and folders) in the current directory).

如果您不希望shell将 * 解释为你需要的所有文件(正如你所注意到的)引用那个论点。

If you do not want your shell to interpret * as all files you need (as you have noticed) to quote that argument.

这篇关于Java命令行参数。使用*作为乘法的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆