2016-10-22 2 views
0

이 작업을 수행하는 동안 정확한 문자 수가 (140)에 도달 할 때까지 루프에서 모음을 삭제하지만 작동하지 않습니다. do while 루프가 작동하고 나서 조건이 충족되면 멈추는 지 확신 할 수 없지만 조건이 충족되면 나머지 코드를 어떻게 실행할 수 있습니까? 미리 감사드립니다.카운트가 작동하지 않을 때까지 문자를 삭제하려면 while 루프를 수행하십시오. Java

public static void main(String[] args) 
{ 
    Scanner scan = new Scanner(System.in); 
    System.out.println("Enter the phrase to be shortened: "); 
    String toCompress = scan.nextLine(); 
    int length = toCompress.length(); 
    System.out.println(length);; 

    do { 
     toCompress = toCompress.replaceAll("[AEIOUaeiou]", ""); 
    }while(length >= 140); 


    System.out.println("Compressed phrase: "); 
    System.out.println(toCompress); 
    int length2 = toCompress.length(); 
    System.out.print(length2);; 
    scan.close(); 

} 
+0

나는 작동하지 않는 것을 이해하지 못합니다. 프로그램을 실행할 때 어떤 결과가 나타 납니까? – Gendarme

+0

[ "그러나 작동하지 않습니다"] (http://importblogkit.com/2015/07/does-not-work/)를 정의하십시오. – Pshemo

+0

BTW :'string.replaceAll (target, replacement)'는'target'의 모든 항목을'replacement'로 대체하기 때문에 루프 안에서 처리하는 것은'replaceAll ("[AEIOUaeiou]", "")'모음 모음을 모두 즉시 바꿀 것이기 때문에. – Pshemo

답변

0

: 지금 당신은 toCompress = toCompress.replaceAll ("[AEIOUaeiou]", "") 올바른 방법을 수행 한 후 길이를 업데이트하지

public static void main(String[] args) 
{ 
    Random RNG = new Random(); //Set up the RNG 
    Scanner scan = new Scanner(System.in); //Set up the scanner 
    System.out.println("Enter the phrase to be shortened: "); 
    String toCompress = scan.nextLine(); 
    //Setup the string builder with the user input 
    StringBuilder shorten = new StringBuilder(toCompress); 

    //Wile the length is greater than or equal to 140,do some conversions then 
    //run the if statement 
    while (shorten.length() >= 140) 
    { 
     int randChar = RNG.nextInt(shorten.length()); 
     char convertToChar = shorten.charAt(randChar); 
     int convertToInt = (int)convertToChar; 

     //If statement choosing which ASCII chars to delete (vowels) 
      if ((convertToInt > 32) || (convertToInt == 65) 
        || (convertToInt == 69) || (convertToInt == 73) 
        || (convertToInt == 79) || (convertToInt == 85)) 
         { 
          shorten.deleteCharAt(randChar); 
         } 
    } 
    System.out.println("Compressed phrase:"); 
    System.out.println(shorten); 
    System.out.println(shorten.length()); 

    scan.close(); 
}} 
0

여기에 while 루프를 사용하는 이유를 잘 모르겠어요 : 여기 내가 사용하고 코드입니다. replaceAll()은 제공 한 정규 표현식/패턴과 일치하는 문자열의 모든 문자를 찾고 두 번째 인수 (이 경우 빈 문자열)의 문자/패턴으로 바꿉니다. 루프로 처리하면 결과가 변경되지 않고이 경우 쓸모가 없습니다.

문자열에 140 개 이상의 비 모음 문자가있는 경우에도 while 루프가 완료되지 않습니다. replaceAll()에 대한 설명서를 찾아서 어떻게 작동하는지 이해하십시오. 유용한 링크 : 당신은가 (140)의 길이에 도달 할 때까지 한 번에 하나 개의 모음을 제거해야

https://www.tutorialspoint.com/java/java_string_replaceall.htm

+0

루프를 사용하려고하는 전체 이유는 140 문자가 도달 한 후 모음을 삭제하지 않고 새로운 구를 인쇄하기를 원하기 때문입니다. – DwemerTech

1

. 나는이 작업을 수행 할 수있는 더 좋은 방법을 알아 냈어요

public static void main(String[] args){ 
    Scanner scan = new Scanner(System.in); 
    String[] vow={"A","a","O","o","E","e","U","u","I","i"}; 
    List<String> vowels=Arrays.asList(vow); 
    System.out.println("Enter the phrase to be shortened: "); 
    String toCompress = scan.nextLine(); 
    int length = toCompress.length(); 
    System.out.println(length); 

    String compressed=""; 

    while(length >= 140&&compressed.length()<140){ 
     String firstLetter=toCompress.substring(0,1); 
     //if the first letter is not a vowel, add it to the compressed string 
     if(!vowels.contains(firstLetter)) compressed.concat(firstLetter); 
     //remove the first letter from toCompress 
     toCompress=toCompress.substring(1); 
     //update the length to the new value 
     length=compressed.length()+toCompress.length();  
    } 
    //After reaching 140 characters, concatenate the rest of toCompress to compressed 
    compressed.concat(toCompress); 

    System.out.println("Compressed phrase: "); 
    System.out.println(compressed); 
    System.out.print(compressed.length()); 
    scan.close(); 
} 
+0

나를 위해,이 오류없이 컴파일하지만 전체 문자열을 삭제합니다. – DwemerTech

관련 문제