如何组合 Switch 语句?

How to combine Switch statements?

我是 Java 的新手,正在完成一项学校作业。目标是创建一个 Java class 来模拟 unix 中的 Echo 工具。如果我输入:

java Echo 'i love you'

它将打印:

i love you

然后有两个命令行选项
-n: 省略尾随的换行符
-e: 启用反斜杠转义的解释。
我还需要能够同时使用 -n 和 -e。

到目前为止,我想出了如何分别实现 -n-e 的方法。这是代码:

public class Echo {

    public static void main(String []args) {

        switch (args[0]) {
            case "-n":
                for (int i=1; i<args.length;i++)
                    System.out.print(args[i]);
                break;

            case "-e":
                for (int i=1; i<args.length; i++) {
                    args[i]=args[i].replace("\t", "\t");
                    args[i]=args[i].replace("\n","\n");
                    System.out.println(args[i]);
                }
                break;
            default:
                for (int i=0; i<args.length; i++)
                System.out.println(args[i]);
        }
    }    
}

我试图更改代码以便能够输入:

java Echo -n -e 'ilove\tyou'

java Echo -e -n 'ilove\tyou'

它根本不起作用。这是我的尝试:

public class Echo {

    public static void main(String []args) {

        if ((args[0].equals("-n") || args[0].equals("-e")) && (args[1].equals("-e") ||args[1].equals("-n"))) {
            for (int i=2; i<args.length; i++) {
                args[i]=args[i].replace("\t", "\t");
                args[i]=args[i].replace("\n","\n");
                System.out.print(args[i]);
            }
        } else {
            switch (args[0]) {
                case "-n":
                    for (int i=1; i<args.length;i++)
                        System.out.print(args[i]);
                    break;
                case "-e":
                    for (int i=1; i<args.length; i++) {
                        args[i]=args[i].replace("\t", "\t");
                        args[i]=args[i].replace("\n","\n");
                        System.out.println(args[i]);
                    }
                    break;
                default:
                    for (int i=0; i<args.length; i++)
                        System.out.println(args[i]);
            }
        }
    }
}

我会这样做:

public class Echo {
    public static void main(String []args) {
        if (args[0].equals("-n")){
            //-n
            if (args[1].equals("-e")) {
                //-n -e
            }
        } else if (args[0].equals("-e")) {
            //-e
            if (args[1].equals("-n")) {
                //-e -n
            }
        } else {
            //the user typed something other than -n or -e
        }
    }
}

公平地说,我一点也不熟悉 Unix 中的 "Echo"-工具,所以我不知道这段代码是否有帮助。注意:您可能希望将“-n”和“-e”都输入到单独的方法中时的代码编写 - 考虑到您将调用此代码两次。

尝试做这样的事情:

public class Echo {

    public static void main(String []args) {
        boolean isTrailingActive = false;
        boolean isEscapingActive = false;
        String inputStr;
        int index = 0;

        // Validates the amount of arguments passed to Echo
        if (args.length == 0 || args.length > 3) {
            return;
        }

        while (index < args.length - 1) {
            // Validates trailing active
            if (args[index].equals("-n")) {
                isTrailingActive = true;
                index++;
            }

            // Validates escaping active
            if (args[index].equals("-e")) {
                isEscapingActive = true;
                index++;
            }
        }

        inputStr = args[index];

        if (isEscapingActive) {
            inputStr = inputStr.replace("\t", "\t").replace("\n","\n");
        }

        if (isTrailingActive) {
            System.out.print(inputStr);
        } else {
            System.out.println(inputStr);
        }
    }
}

您需要注意标志位于 args[] 中的位置以及字符串所在的位置。由于您的 args[] 的长度可以为 1(无标志),因此您不能随意访问 args[1]

the comment by RC 中所述,先找到标志然后再应用它们的效果要容易得多。我假设标志必须位于字符串之前,因此可以保证字符串位于最后一个单元格中。从那里,向后看旗帜。

public class Example {

    public static void main(String[] args) {

        boolean hasE = false;
        boolean hasN = false;
        String message = args[args.length - 1];

        for (int i = args.length - 2; i >= 0; i--) {
            switch (args[i]) {
                case "-n": hasN = true; break;
                case "-e": hasE = true; break;
            }
        }

        if (hasE)
            message = message.replace("\n", "\n").replace("\t", "\t");
        if (hasN)
            System.out.print(message);
        else
            System.out.println(message);
        System.out.println("Test");
    }
}

备注:

  • 我没有做输入验证(你没有在你的作业或代码中提到它,我不想做整个作业)。提示:检查 args.length 是否为 0 或多于 3。
  • 我假设您只需要转义制表符和换行符。如果您需要转义更多内容,则需要更复杂的正则表达式。
  • 我在最后加了一个打印,方便查看-n的效果。

有两种解决方法:
1 ) 对 CommandLine 使用 API,有很多。

2 ) 在您的参数上执行一个 foreach ...您可以将最后一个参数管理为字符串以回显或从每个参数构建它

boolean optionNewline = false;  
boolean optionE = false;  
StringBuilder sb = new StringBuilder();

for(String arg : args) {
    if ("-n".equals(arg)) {
        optionNewline = true;
    }
    else if ("-e".equals(arg)) {
        optionE = true;
    }
    else {
        // TEXT
        if(sb.length() > 0) {
            sb.append(' ');    //space between text part ?
        }
        sb.append(arg);
    }
} // foreach
// DO MY JOB on SB.toString();