2015-01-15 45 views
-4

下面是計算元音的代碼。如何顯示元音而不是隻計算它們?如何顯示元音而不是隻計算元音(元音)?

System.out.println("Enter the String:"); 

String text = we.readLine(); 

int count = 0; 
for (int i = 0; i < text.length(); i++) { 
    char c = text.charAt(i); 
    if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') { 
     count++; 
    } 
} 
System.out.println("The number of vowels in the given String are: " + count); 
+1

你怎麼顯示消息'元音中的數給定的字符串是:'? – 2015-01-15 16:48:23

+0

我甚至不完全確定你在這裏問什麼。你是否想要顯示每個元音出現的次數? – 2015-01-15 16:52:52

+0

而不是統計多少元音,輸出必須顯示元音。編程元音的例子是:o a i – 2015-01-15 16:57:14

回答

0

對於備用,可以創建元音的字符數組,轉動String轉換爲字符數組,並比較每個索引:

char[] vowels = {'a', 'e', 'i', 'o', 'u'}; 
String text = "Programming"; 

for (char c : text.toCharArray()) { 
    for (char vowel : vowels) { 
     if (c == vowel) { 
      System.out.println(text + " contains the vowel: " + vowel); 
     } 
    } 
}