if(c =='a'|| c =='e'|| c =='i'|| c =='o'|| c =='u'){ count ++;字符檢查
當我給出上述說法時,只有當給定的單詞是小寫的(即:輸入:朋友輸出2元音)時,它才返回一個單詞中的元音數量。我想知道,即使我給大寫或混合它應該返回元音的數量。怎麼做?
if(c =='a'|| c =='e'|| c =='i'|| c =='o'|| c =='u'){ count ++;字符檢查
當我給出上述說法時,只有當給定的單詞是小寫的(即:輸入:朋友輸出2元音)時,它才返回一個單詞中的元音數量。我想知道,即使我給大寫或混合它應該返回元音的數量。怎麼做?
您可以使用測試如:
Character.toLowerCase(c) == 'a'
改爲。
雅我明白了,謝謝! – Sumithra 2010-10-13 07:21:06
@Sumithra:請注意,這將在土耳其語語言環境中失敗,因爲它在大寫字母和小寫字母中都有點和無點,所以Character.toLowerCase('I')=='ı'和Character.toUpperCase('i' )=='İ'http://en.wikipedia.org/wiki/Dotted_and_dotless_I – 2010-10-13 07:34:19
這是如何失敗? – 2012-10-09 17:41:18
如果你想獲得大寫或小寫的一種簡單的方法似乎是以下幾點:
遍歷這個詞的所有字符,並做如下測試:
if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u'
|| c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
{ count++ };
Here有一個完整的例子 - 注意他們在檢查元音之前將字符串轉換爲小寫字母。
你也可以用不區分大小寫的正則表達式,例如嘗試從http://www.shiffman.net/teaching/a2z/regex/:
String regex = "[aeiou]";
Pattern p = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);
int vowelcount = 0;
Matcher m = p.matcher(content); // Create Matcher
while (m.find()) {
//System.out.print(m.group());
vowelcount++;
}
正則表達式的另一個優點是它可以很容易地提取到一個單獨的方法中,並可以重複用於其他模式。 – Ruben 2010-10-13 07:18:54
+1最優雅的解決方案imho – 2010-10-13 07:33:25
嗯,衆所周知,regexp是*最快的解決方案。我的意思是,開發明智... – 2010-10-13 15:28:25
我告訴你一件事,如果你給了放在大寫然後在你的代碼接受給出了投入小寫。
如果字符串str = '朋友'
手柄str.Tolower(); 請嘗試這個我認爲你的問題解決了。
一個succint,簡單的方法來做到這一點而不做10個比較:
if ("aeiouAEIOU".indexOf(c) != -1) { count++; }
不是最有效的解決方案,但也許是最短的? :)
int vowelCount = (inputString+" ").split("(?i)[aoeuiy]").length - 1
順便說一句,我是唯一一個計數'y'作爲一個wovel? ;)
如何:
if (Arrays.asList('a', 'e', 'i', 'o', 'u').contains(Character.toLowerCase(c))) {
...
}
我也要靜態最終的名單。
當聲明爲static final時,該列表應該包含在'Collections.unmodifiableList(..)'中。 – whiskeysierra 2010-10-13 15:16:35
這是一個非常簡單的工具類:
public class Main {
public static int countChars(String string, Character... characters) {
return countChars(string, new HashSet<Character>(Arrays.asList(characters)));
}
public static int countChars(String string, Set<Character> characters) {
int count = 0;
for(int i = 0; i < string.length(); i++){
if(characters.contains(string.charAt(i))){
count++;
}
}
return count;
}
public static int countCharsIgnoreCase(String string, Character... characters) {
return countCharsIgnoreCase(string, new HashSet<Character>(Arrays.asList(characters)));
}
public static int countCharsIgnoreCase(String string, Set<Character> characters) {
Set<Character> finalCharacters = new HashSet<Character>();
for (Character character : characters) {
finalCharacters.add(Character.toUpperCase(character));
finalCharacters.add(Character.toLowerCase(character));
}
return countChars(string, finalCharacters);
}
}
利用番石榴的CharMatcher:
private final CharMatcher vowels = CharMatcher.anyOf("aeiouAEIOU");
...
// somewhere in your method:
if (vowels.matches(c)) {
...
}
沒有看到一個Set
但:-(
static Character charvowels[] = { 'A','I','U','E','O','a','i','u','e','o' };
static Set<Character> vowels = new HashSet<Character>(Arrays.asList(charvowels));
...
public void countVowel(char c) {
if (vowels.contains(c)) count++;
}
+1爲您的問題啓發了這麼多不同的答案。 – DerMike 2010-10-13 07:32:23
對於使用「最小概念改變原則」,感謝你的邁克 – Sumithra 2010-10-13 10:32:11