我正在編寫一個代碼,用於在我編寫和計數它們的句子中查找所有元音'a' 'i' 'u' 'e' 'o'
。找到一個句子中所有元音的出現
該代碼將忽略元音(大寫或小寫)的情況。由於我不知道如何使元音不區分大小寫,我只是將所有文本轉換爲小寫並計算元音。我想知道是否有另一種方法可以將所有大寫字母文本轉換爲小寫字母並計算元音。謝謝大家的意見,我非常感謝。這是我的代碼。
import java.util.Scanner;
public class VowelCount
{
public static void main(String[] args)
{
//create scanner object
Scanner input = new Scanner (System.in);
//create vowel array
char[] vowel = new char[]{'a', 'e', 'i', 'o', 'u'};
int[] countVowel = new int[5];
String yourSentence;
//print message
System.out.print("Enter your word here:");
//set word entered as next input
yourSentence = input.nextLine();
String actualSentence = yourSentence.toLowerCase();
for (int j = 0; j < actualSentence.length(); j++)
{
char c =actualSentence.charAt(j);
if(c=='a')
countVowel[0]++;
else if(c=='e')
countVowel[1]++;
else if(c=='i')
countVowel[2]++;
else if(c=='o')
countVowel[3]++;
else if(c=='u')
countVowel[4]++;
}
for (int i = 0; i <countVowel.length; i++)
{
System.out.println("Count of vowel " + vowel[i] + "=" + countVowel[i]);
}
}
}
哇,我不知道我們可以設置2個例子來像上面那樣做一個計數器。非常感謝你幫助我。 – NumbNuts 2014-11-28 08:24:38