【发布时间】:2017-11-07 01:11:04
【问题描述】:
在我输入的单词中显示元音时有一点问题。我有元音= AEIOU。我在想这可能是问题所在,但我不确定,也不知道如何解决。
这是它给我的:
输入一个单词:ANTARTICA
这是ANTARTICA中的元音:AEIOU
元音数:4
过程完成。
如您所见,它计算了元音,我只想显示它们。我该怎么做? (我是一个菜鸟编码......)
import static java.lang.System.*;
import java.util.*;
public class Java2106{
public static void main(String[] args) {
new Solution();
}}
class Solution
{
String word;
int count;
String vowels;
Solution()
{
input();
output();
}
public void input()
{
Scanner scan = new Scanner(in);
out.print("Enter a word: ");
word = scan.nextLine();
}
public void output()
{
vowels = "AEIOU";
count = 0;
out.println();
out.print("Here are the vowels in " + word + ": " + vowels);
for (int x = 0; x < word.length(); x++)
{
String let = word.substring(x,x+1);
if (vowels.contains(let))
count++;
}
out.println("\nVowel count: " + count);
}
}
【问题讨论】:
-
如果你想显示你在文本中找到的完全相同的元音,你应该有一个额外的变量来存储元音。例如某种
List。然后每次找到元音时,只需将其推入此列表并最终打印出来。
标签: java string for-loop count