【问题标题】:Looping through all main() arguments循环遍历所有 main() 参数
【发布时间】:2014-04-04 17:32:03
【问题描述】:
  • 应用程序将使用 主要论点。
  • 它将确定是否输入了每个输入字符串 以辅音、元音、奇数、偶数或 特殊符号。
  • 应该也能统计个数 每个单词输入的字符数。

到目前为止,这就是我所拥有的:

public static void main(String[] args) {
    if(args.length > 0) {
        String regExVowels = ".*[AEIOUaeiou]$";
        // regEx Strings

        char[] caMainArg = null;
        String strMainArg = null;

        for(String arg: args) {
            // Convert each String arg to char array
            caMainArg = arg.toCharArray();

            // Convert each char array to String
            strMainArg = new String(caMainArg);
        }

        System.out.print(strMainArg + " - " + strMainArg.length());

        // if-else conditions

    } else {
        System.out.println("No arguments passed!");
    }
}

它有效,但它只接受最后一个参数。例如:

Eclipse > 运行配置...> 参数

kate middleton sep30 jan25 `

它只会输出:

` - 1 - special character

我想要的输出是:

kate - 4 - vowel
middleton - 9 - consonant
sep30 - even number
jan25 - odd number
` - 1 - special character

我不确定如何遍历参数并打印适当的结果。

【问题讨论】:

标签: java for-loop foreach arguments main


【解决方案1】:

你过早地关闭了 for 循环。

你这样做:

   for(String arg: args) {
        // Convert each String arg to char array
        caMainArg = arg.toCharArray();

        // Convert each char array to String
        strMainArg = new String(caMainArg);
    }
    System.out.print(strMainArg + " - " + strMainArg.length());

    if(regExVowels.matches(strMainArg)) {
        System.out.print(" - vowel");

    } else if(regExUpperConsonants.matches(strMainArg) ||

    .....

你需要做的:

   for(String arg: args) {
        // Convert each String arg to char array
        caMainArg = arg.toCharArray();

        // Convert each char array to String
        strMainArg = new String(caMainArg);
        System.out.print(strMainArg + " - " + strMainArg.length());

        if(regExVowels.matches(strMainArg)) {
            System.out.print(" - vowel");

        } else if(regExUpperConsonants.matches(strMainArg) ||

        ....

    }

【讨论】:

  • 非常感谢您的帮助。标记为已接受的答案。
【解决方案2】:

放,

    System.out.print(strMainArg + " - " + strMainArg.length());

    if(regExVowels.matches(strMainArg)) {
        System.out.print(" - vowel");

    } else if(regExUpperConsonants.matches(strMainArg) ||
            regExLowerConsonants.matches(strMainArg)) {
        System.out.print(" - consonant");

    } else if(regExEven.matches(strMainArg))    {
        System.out.print(" - even number");

    } else if(regExOdd.matches(strMainArg)) {
        System.out.print(" - odd number");

    } else {
        System.out.print(" - special character");
    }

在你的循环中,你已经拥有循环通过参数的底部,你的“for(String arg:args){”行向下大约 10 行。

【讨论】:

    【解决方案3】:

    是的,因为你应该移动这个:

    System.out.print(strMainArg + " - " + strMainArg.length());
    
    if(regExVowels.matches(strMainArg)) {
        System.out.print(" - vowel");
    
    } else if(regExUpperConsonants.matches(strMainArg) ||
        regExLowerConsonants.matches(strMainArg)) {
        System.out.print(" - consonant");
    
    } else if(regExEven.matches(strMainArg))    {
        System.out.print(" - even number");
    
    } else if(regExOdd.matches(strMainArg)) {
        System.out.print(" - odd number");
    
    } else {
        System.out.print(" - special character");
    }
    

    在循环内部,因为要检查所有参数,而不仅仅是最后一个。

    【讨论】:

    • 谢谢,我相信最后的}else{一定不能包含。
    猜你喜欢
    • 2017-03-09
    • 1970-01-01
    • 2021-11-27
    • 2018-12-02
    • 1970-01-01
    • 2017-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多