我無法弄清楚這段代碼有什麼問題。無論我提供什麼作爲輸入,代碼打印我作爲輸出。任何幫助表示讚賞。程序打印最多元音字數
public class VowelClass{
public static void main(String args[]){
String input;
System.out.println("Please enter your sentence: ");
Scanner scan = new Scanner(System.in);
input = scan.nextLine();
int maxVCount = 0;
String mostVowels = null;
String[] words = input.split("");
for (String word : words) {
int vCount = 0;
word = word.toLowerCase();
for (int i=0; i<word.length(); i++){
char x = word.charAt(i);
if (x == 'a' || x == 'e' || x == 'i' || x == 'o' || x == 'u'){
vCount++;
}
}
if (vCount > maxVCount) {
maxVCount = vCount;
mostVowels = word;
}
}
System.out.println("Word with most vowels is:");
System.out.println(mostVowels);
}
}
你可以改變你的'input.split(「」)''input.split(「」);' – Orin
謝謝!這是尷尬 – wibwaj
在未來,嘗試調試或SOP門控你的輸出,即'System.out.println(word);'出於理智檢查的目的。最快的方式找到一個錯字。 – Compass