這是我的代碼,它工作正常(我認爲),但如果我想輸入與其他字符如「。,*:;」的句子。那麼他們肯定會被算作輔音。試圖計數元音,輔音和空格,但忽略其他字符的
有沒有辦法縮短這個沒有輸入所有不是元音的字符?
另外,我知道我可以使用「IGNORECASE()」而不是鍵入出元音的所有資金版本,但實現之後,我打他們,我太固執的改變:d
public void compute() {
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u' || str.charAt(i) == 'A'
|| str.charAt(i) == 'E' || str.charAt(i) == 'I' || str.charAt(i) == 'O' || str.charAt(i) == 'U') {
vowels++;
} else if (str.charAt(i) == ' ') {
spaces++;
} else {
cons++;
}
}
}
這看起來像..如果(str.charAt(i).Character.isLetter == true)??我從來沒有用過這種方法,這將是我的第一次猜測,雖然 – Adam
@亞當不,它應該是︰Character.isLetter(str.charAt(i)) –
真棒!這沒有把戲好,謝謝! – Adam