2016-09-10 3 views
1

으로 읽어들입니다. 도움이 필요해. 저는 사용자가 텍스트를 입력하고, 텍스트를 반대로하고, 텍스트의 모음 수를 읽고, 역행 된 텍스트에 얼마나 많은 모음이 있는지 알려주고 있습니다.자바 문자열의 모음 수를

public static void main(String[] args) 
{ 
System.out.println ("Please type in whatever you want "); 
Scanner type_input = new Scanner (System.in); 
Scanner type_input = new Scanner (System.in); 
StringBuilder type_output = new StringBuilder(); 
type_output.append(type_hold); 
type_output=type_output.reverse(); 
System.out.println("Is this what you types in? " + type_output); 

for(int vowel_num = 0; vowel_num< type_hold.length(); vowel_num++) 
    { 
     if((type_hold.charAt(vowel_num) =='a')||(type_hold.charAt(vowel_num) =='e')|| 
      (type_hold.charAt(vowel_num) =='o')||(type_hold.charAt(vowel_num) =='i')|| 
      (type_hold.charAt(vowel_num) =='u')){  

     System.out.println("There are " + vowel_num + " vowels in " + type_hold); 
    } 

하지만 실행시 입력 할 때 다음과 같이 표시됩니다. 내가 어디서 엉망인지 모르겠다.

run: 
Please type in whatever you want 
hello 
Is this what you types in? olleh 
There are 1 vowels in hello 
There are 4 vowels in hello 

편집 : 알아 냈습니다. 도와 주신 모든 분들께 감사드립니다!

+7

나는 반전 된 텍스트 모음의 수는 원래 텍스트에서 모음의 수와 동일하다는 걸거야 ... – ajb

+0

@ajb 나는 그 의견에 좋은 웃음을 가지고 있었다. 그러나 나는 그가 여기에 2 개의 연습 과제를 가지고 있다고 생각한다. - 텍스트를 역순으로 바꾸고 모음을 세어 라. 나는 그가 원래의 *와 * 역전 된 텍스트로 그들을 세우려고했다고 생각하지 않는다. –

+2

왜'type_input ='라인이 두 번입니까? 다른 문제는 'vowel_num'이 값의 수를 세지 않고 오히려 그 위치를 제공한다는 것입니다. –

답변

3
  1. type_hold은 무엇입니까? 당신이 그것을 인스턴스화하는 것을 보지 못했고 그것을 사용하고 있습니다.
  2. 무엇 vowel_num? 문자열을 반복하는 인덱스? 또는 당신이 세었던 모음의 수? 당신은 문자열에서 모음을 계산하기 위해 수행해야 무엇

:

index 가정은 우리가 현재 스캔하는 문자열의 인덱스, vowel_count 당신이 발생한 모음의 수입니다.

int vowel_count = 0; 
for(int index = 0; index < type_hold.length(); index++) { 
    if((type_hold.charAt(index) =='a') || 
     (type_hold.charAt(index) =='e') || 
     (type_hold.charAt(index) =='o') || 
     (type_hold.charAt(index) =='i') || 
     (type_hold.charAt(index) =='u')){ 

     // character at vowel_index is a vowel. 
     // you have encountered 1 more vowel! 
     System.out.println("Character at " + index + " is a vowel."); 
     vowel_count ++; 
    } 
} 

System.out.println("There are " + vowel_count + " vowels in " + type_hold); 
+0

'vowel_index'의 이름을'inputIndex' 또는 그와 비슷한 이름으로 바꿀 것을 제안합니다. –

+0

감사합니다. 당신의보기를보고 나는 어디에 내가 엉망이되었는지 깨닫는다. _hold 유형은 초기 변수로 스캐너 변수 new_input의 입력을 유지하기위한 것이므로 StringBuilder 변수 type_output에서 사용할 수 있습니다. 내가 할 수있는 더 좋은 방법이 있다고 확신하지만, 나는 아직 어떻게해야할지 모른다. 따라서 vowel_index는 보았던 모음의 각 인스턴스에 대해 정수에 값을 추가하고 for 루프는 vowel_num에 값을 추가합니다. –

+0

유감스럽게 생각합니다. vowel_index가 인덱스라고 말할 때, 문자열을 반복 할 수있는 색인이라는 의미입니다. 따라서 각 for 루프에서 문자열의 다음 문자를보기 위해 증가합니다. –

-1

수정 된 코드; 보세요;

public static void main(String[] args) 
{ 
    System.out.println ("Please type in whatever you want "); 
    Scanner type_input = new Scanner (System.in); 
    Scanner type_input = new Scanner (System.in); 
    StringBuilder type_output = new StringBuilder(); 
    type_output.append(type_hold); 
    type_output=type_output.reverse(); 
    System.out.println("Is this what you types in? " + type_output); 

    int count=0; 
    for(int vowel_num = 0; vowel_num< type_hold.length(); vowel_num++) 
    { 
    if((type_hold.charAt(vowel_num) =='a') ||(type_hold.charAt(vowel_num) =='e')|| 
     (type_hold.charAt(vowel_num) =='o')||(type_hold.charAt(vowel_num) =='i')|| 
     (type_hold.charAt(vowel_num) =='u')){  

    //System.out.println("There are " + vowel_num + " vowels in " + type_hold);      Instead of this 

    count++; 
    } 

System.out.println("There are " + count + " vowels in " + type_hold); 
} 

작업을 완료해야합니다.

+2

1. 컴파일하지 않습니다. // 2. 코드를 "수정"해야 할뿐만 아니라, 처음부터 무엇이 잘못되었는지 설명해야합니다. –

3

Java 8 스트림은 문자열 내부의 문자 배열에서 사용할 수 있습니다.

StringBuilder type_hold = new StringBuilder(); 
long vowel_num = type_hold.toString().toLowerCase().chars() 
     .filter(it -> "aeiou".indexOf(it) != -1).count(); 
+0

스티븐은 프로그래밍 초보자이며 기본 학습 만한다고 가정합니다. IMHO 스트림과 람다는 초보자를위한 것이 아닙니다. –

+1

필자는 'it -> "aeiou".indexOf (it)> = 0';) +1을 사용하겠다. –

+0

@peter 우수, 감사합니다. – Adam

관련 문제