2014-11-23 25 views
2

我正在編寫一個代碼,用於在我編寫和計數它們的句子中查找所有元音'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]); 
     } 

    } 
} 

回答

0

你可能最好使用switch語句,因爲它是非常可讀的,可以很容易地進行優化由編譯器

yourSentence = input.nextLine(); 
// String actualSentence = yourSentence.toLowerCase(); 

for (int j = 0; j < yourSentence.length(); j++) 
{ 
    switch(yourSentence.charAt(j)) { 
     case 'a': 
     case 'A': 
      ++countVowel[0]; 
      break; 
     case 'e': 
     case 'E': 
      ++countVowel[1]; 
      break; 
     case 'i': 
     case 'I': 
      ++countVowel[2]; 
      break; 
     case 'o': 
     case 'O': 
      ++countVowel[3]; 
      break; 
     case 'u': 
     case 'U': 
      ++countVowel[4]; 
      break; 
    } 
} 
+0

哇,我不知道我們可以設置2個例子來像上面那樣做一個計數器。非常感謝你幫助我。 – NumbNuts 2014-11-28 08:24:38

2

你改變輸入yourSentence爲大寫

String newResult = changeCase.toUpperCase(); 

,然後問,如果最後一句包含任何你想要的性格特徵的。

這完全不是你想要做的。

我會給你一個示例解決方案,但還有更多你可以做的。

String newResult = changeCase.toUpperCase(); 

if(newResult.contains("A") ||newResult.contains("E") || newResult.contains("O") || newResult.contains("I") || newResult.contains("U")) 
    // Your code 

現在,如果你想指望它,你可以使用indexOf和刪除事件:

while(newResult.indexOf("U") > -1)){ 
    counter++ 
    newResult.replaceFirst("U", "!"); 

} 
+0

謝謝你幫助我,不知何故,當我編寫像你的while循環一樣的格式;我得到錯誤 – NumbNuts 2014-11-26 21:54:32

+0

這裏是錯誤字符串newResult = changeCase.toUpperCase(); \t \t //打印消息 \t \t System.out.printf(「%s \ n」,actualWord); \t \t int counter; \t \t //條件 \t \t如果(newResult.contains( 「A」)|| newResult.contains( 「E」)|| newResult.contains( 「I」) \t \t || newResult.contains(「O 「)|| newResult.contains(」U「)) \t \t { \t \t \t System.out.println(newResult); \t \t \t \t \t \t而(newResult.indexOf( 「A」)> -1;計數器++) \t \t \t { \t \t \t \t newResult.replaceFirst( 「A」, 「!」); (「found vowel \」a \「appear」); \t \t \t \t System.out.println(newResult.indexOf('a')); \t \t \t} – NumbNuts 2014-11-26 22:12:25

+0

該錯誤與counter ++有關。我宣佈櫃檯,放;在計數器++之後,只是關於計數器的一般想法,但不知何故它仍然得到錯誤 – NumbNuts 2014-11-26 22:19:54

相關問題