2016-07-05 76 views
0

我无法弄清楚这段代码有什么问题。无论我提供什么作为输入,代码打印我作为输出。任何帮助表示赞赏。程序打印最多元音字数

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

你可以改变你的'input.split(“”)''input.split(“”);' – Orin

+0

谢谢!这是尴尬 – wibwaj

+0

在未来,尝试调试或SOP门控你的输出,即'System.out.println(word);'出于理智检查的目的。最快的方式找到一个错字。 – Compass

回答

1

input.split(""); =>input.split(" ");

需要的话之间分裂,因此在分割方法使用 '空间' 字符。

+0

尴尬!非常感谢Alex。 – wibwaj

+0

哪个单词的元音最多:flyby,cwmy,yellow或者strength? – FredK

+0

确实。黄色是赢家:) – wibwaj