【发布时间】:2019-08-31 14:23:45
【问题描述】:
我想打印包含最多元音的单词。但问题是包含最大数量的句子的最后一个单词没有打印出来。请帮我解决这个问题。我的代码如下。
当我输入输入'Happy New Year' 时,输出为'Yea'。但我希望我的输出为'Year'
import java.util.Scanner;
public class Abcd {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter The Word : ");
String sentence = sc.nextLine();
String word = "";
String wordMostVowel = "";
int temp = 0;
int vowelCount = 0;
char ch;
for (int i = 0; i < sentence.length(); i++) {
ch = sentence.charAt(i);
if (ch != ' ' && i != (sentence.length() - 1)) {
word += ch;
ch = Character.toLowerCase(ch);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowelCount++;
}
} else {
if (vowelCount > temp) {
temp = vowelCount;
wordMostVowel = word;
}
word = "";
vowelCount = 0;
}
}
System.out.println("The word with the most vowels (" + temp + ") is: " + " " + wordMostVowel);
}
}
【问题讨论】:
-
谢谢,但我该怎么办?请给我代码来解决这个问题
标签: java