命令行参数java *正在重载以获取文件名 [英] command line arguments java * is being overloaded to pick up filenames

查看:72
本文介绍了命令行参数java *正在重载以获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java语言编写最简单的计算器,它可以获取命令行参数,除乘法功能外,其他所有功能都可以正常工作。由于某种原因,它会从当前位置获取文件名。我绝对不知道为什么会这样,以及如何关闭它……谷歌没有帮助。

I am writing the most simple calculator in java that picks up command line arguments.Everything works fine, except the multiplication function. For some reason it picks up names of the files from the current location. I have absolutely no idea why this happening, and how to turn it off...google was no help.

public class Calculate {

    public static void main(String[] args) {

        System.out.println(args[0] + " " + args[1] + " " + args[2]);

        if (args.length != 3) {
            System.out.println("<number1> <operator> <number2>");
        } else {

            final int num1 = Integer.parseInt(args[0]);
            final int num2 = Integer.parseInt(args[2]);
            switch (args[1]) {
            case "+":
                System.out.println(num1 + num2);
                break;
            case "-":
                System.out.println(num1 - num2);
                break;
            case "*":
                System.out.println(num1 * num2);
                break;
            case "/":
                System.out.println(num1 / num2);
                break;
            }
            System.out.println("\n");
        }
    }
}

推荐答案

此与Java无关,但是与命令行有关,您需要使用:

This has nothing to do with java, but with the command line, you need to use:

java Calculate 2 "*" 3

原因是 * 由命令行作为通配符:它被文件夹中的所有文件替换,幸运的是只有一个这样的文件。这是几乎所有(基本)shell的默认行为。但是,如果您在单引号('*')或双引号( * )之间编写它,则Shell会执行不是 intepret 它,而是将其作为字符传递。

The reason is that * is interpreted by the command line as a wildcard: it is replaced by all files in the folder, and luckily there is only one such. It is the default behavior of almost all (basic) shells. If you write it however between single ('*') or double ("*") quotes, the shell does not intepret it, but passes it as a character.

所以基本上发生的是您的呼叫被 cmd替换到:

So what basically happens is that your call, is replaced by the cmd to:

java Calculate 2 <files in the directory> 3

换句话说, java 不会甚至不知道您曾经使用过通配符,因为在您调用该程序之前之前已将其替换。

In other words, java doesn't even know you have ever used a wildcard, since it has been replaced before you called the program.

可以找到更多信息此处(尽管该网站是为设计的Linux shell,这些想法通常适用)。

More info can be found here (although the website is designed for Linux shells, the ideas are generally applicable).

EDIT

@PatriciaShanahan认为,您还可以将整个表达式放在引号中,例如:

As @PatriciaShanahan argues you can also place the entire expression in quotes, like:

java Calculate "2 * 3"

但是请注意,在这种情况下, args [0] 将包含 2 * 3 (而 args 的长度将为 1 ,因此您不能从shell解析表达式的部分中受益),因此您需要自己将字符串细分为多个部分。而且您不会完全删除引号。

Note however that in that case, args[0] will contain "2 * 3" (and the length of args will be 1, thus you can't take benefit from the shell parsing your expression partly), so you will need to subdivide the string into parts yourself. And you don't drop the quotes completely.

这篇关于命令行参数java *正在重载以获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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