【问题标题】:How to display the vowels instead of just counting it(vowels)?如何显示元音而不是仅仅数它(元音)?
【发布时间】:2015-01-15 16:47:50
【问题描述】:

这是一个计算元音的代码。如何显示元音而不只是数它们?

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

【问题讨论】:

  • 你是如何显示消息The number of vowels in the given String are:的?
  • 我什至不完全确定你在这里问什么。您想显示每个元音出现的次数吗?
  • 而不是计算有多少元音,输出必须显示元音。例如“编程”元音是:o a i

标签: java arrays string


【解决方案1】:

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

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

【讨论】:

    最近更新 更多