我应该编写一个简单的 java 应用程序,计算用户输入的字符串中的每个元音,并输出每个元音出现的次数。
我不明白为什么我的代码会检查字符串中的每个单词。我为每个单词获得了适量的元音。这是我所拥有的:
import java.util.Scanner;
public class VowelAnalyst
{
//
//
public static void main (String[] args)
{
String userString;
int aCount = 0, eCount = 0, iCount = 0, oCount = 0, uCount = 0;
char vowels;
Scanner scan = new Scanner (System.in);
System.out.println ("enter string:");
userString = scan.nextLine();
for (int count = 0; count < userString.length(); count++)
{
vowels = userString.charAt(count);
switch (vowels)
{
case 'a':
aCount++;
break;
case 'e':
eCount++;
break;
case 'i':
iCount++;
break;
case 'o':
oCount++;
break;
case 'u':
uCount++;
break;
default:
System.out.println ("Please enter valid string.");
}
System.out.println ("a: " +aCount);
System.out.println ("e: " +eCount);
System.out.println ("i: " +iCount);
System.out.println ("o: " +oCount);
System.out.println ("u: " +uCount);
}
}
}