Java 遍历所有main()参数 应用程序将使用 主要参数。 它将确定是否输入每个输入字符串 以辅音、元音、奇数、偶数或字母结尾 特殊符号。 它还应该能够计算 每个字输入的字符数

Java 遍历所有main()参数 应用程序将使用 主要参数。 它将确定是否输入每个输入字符串 以辅音、元音、奇数、偶数或字母结尾 特殊符号。 它还应该能够计算 每个字输入的字符数,java,for-loop,foreach,arguments,main,Java,For Loop,Foreach,Arguments,Main,到目前为止,我的情况如下: public static void main(String[] args) { if(args.length > 0) { String regExVowels = ".*[AEIOUaeiou]$"; // regEx Strings char[] caMainArg = null; String strMainArg = null; for(String arg:

到目前为止,我的情况如下:

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

我不确定如何循环使用参数并打印适当的结果。

您过早地关闭了
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) ||

        ....

    }

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

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");
}
在循环内部,因为您希望检查所有参数,而不仅仅是最后一个参数。

Put

    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行。

这是家庭作业吗?如果是,请这样标记。@Jonno_FTW.@Pshemo谢谢,我不知道有什么变化。谢谢你,我相信最后一行。}{不得包含在内。非常感谢您的帮助。标记为接受答案。