2014-03-03 255 views
3
  • 應用程序將使用 主要參數請求一系列輸入字/字符串。
  • 它將確定輸入的每個字符串是否輸入 以輔音,元音,奇數,偶數或特殊符號結尾。
  • 它還應該能夠計算每個字輸入的字符數 。

到目前爲止,這是我所:循環遍歷所有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: 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 

我不確定如何遍歷參數並打印適當的結果。

+1

這是功課嗎?如果是這樣,請標記它。 –

+1

@Jonno_FTW ['家庭作業標籤已棄用](http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially- depprecated)。 – Pshemo

+0

@Pshemo謝謝,我沒有意識到這種變化。 –

回答

3

您過早地關閉了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) || 

     .... 

    } 
+0

非常感謝您的協助。標記爲接受的答案。 – silver

0

是的,因爲你應該將這樣的:

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"); 
} 

你循環中,因爲要檢查所有你的論點,不僅是最後一個。

+0

謝謝,我相信最後一個} else {不能包含在內。 – silver

1

說,

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"); 
    } 

裏面你對你已經有過爭論,做循環中的底部循環,你的「的(字符串ARG:參數){」下行線約10條線路。