我从用户那里得到了 3 个字符串作为输入。然后计算每个单词中的元音并打印如下。如果元音计数 = 0 和 1,则打印 0。如果元音计数 = 2,则打印 2。如果元音计数更大大于或等于 3 然后打印 3。我试过这段代码。
Scanner in = new Scanner(System.in);
String[] str = new String[3];
for (int i = 0; i<3; i++)
str[i] = in .nextLine();
for (int j = 0; j<3; j++) {
String s1 = str[j];
String s = s1.toLowerCase();
int count = 0;
for (int i = 0; i<s.length(); i++)
{
if (s.charAt(i) == 'a' || s.charAt(i) == 'e' || s.charAt(i) == 'i' || s.charAt(i) == 'o' || s.charAt(i) == 'u') {
count++;
}
if (s.charAt(i) == ' ') {
if (count == 0||count == 1) {
System.out.print("0");
} else if (count == 2) {
System.out.print("1");
} else {
System.out.print("3");
}
count = 0;
}
}
if (count == 0||count == 1) {
System.out.println("0");
} else if (count == 2) {
System.out.println("1");
} else {
System.out.println("3");
}
}
但是有一个条件只打印 3 个单词的元音计数,即使用户输入的字符串超过 3 个单词。例如,如果用户给出字符串“hi hello all, how are you, I am fine and u”它仅像这样打印“010 011 001”,但此代码打印为“010 011 00100”。现在我如何更改代码以仅打印 3 个单词而不是超过 3 个单词的元音计数?
爪哇
holdtom
九州编程
饮歌长啸
相关分类