如何显示元音而不是仅仅计算它(元音)?

How to display the vowels instead of just counting it(vowels)?

这是计算元音的代码。如何显示元音而不是仅仅计算它们?

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);

作为替代方案,您可以创建元音字符数组,将字符串转换为字符数组并比较每个索引:

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);
        }
     }
}