2015-01-15 118 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); 
     } 
    } 
}