2014-03-03 2 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!"); 
    } 
} 

그것은 작동하지만, 마지막 인수를 사용합니다. 예를 들면 :

이클립스> 실행 구성 ...> 인수

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-official-deprecated). – Pshemo

+0

@Pshemo 감사합니다. 나는 그 변화를 알지 못했습니다. –

답변

3

for 루프를 너무 일찍 종료하십시오.

당신이 할 모든 검사를 원하기 때문에, 당신 루프 내에서

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) { 
     // 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

네를 :

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

감사합니다. 마지막으로 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"); 
    } 

, 당신의 "(문자열 인수 : 인수)에 대한 {"아래로 약 10 라인 라인.

관련 문제