import java.util.*;
public class VowelCounter
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Input a series of characters: ");
String letters = keyboard.next();
int count = 0;
for (int i = 0; i < letters.length(); i++)
{
char characters = letters.charAt(i);
if (isVowel(characters) == true)
{
count++;
}
}
System.out.println("The number of vowels is: " + count);
}
public static boolean isVowel(char characters)
{
boolean result;
if(characters=='a' || characters=='e' || characters=='i' || characters=='o' || characters=='u')
result = true;
else
result = false;
return result;
}
}
該代碼有效,但即使假設輸入「Spring break only only once once year。」。如果我使用空格,我的程序只能找到Spring的元音。我如何做到這一點,所以它會跳過空格並閱讀整個句子。編寫一個程序,提示用戶輸入一個字符序列並輸出元音的數字
而不是使用'字母=鍵盤。鍵盤();'使用'keyboard.nextLine()' – gtgaxiola