2014-01-31 44 views
1

我做了一個程序來算元音和輔音的輸入的字符串數量:計數元音與輔音

 Scanner in = new Scanner(System.in); 


System.out.print("Enter a string "); 
    String phrase = in.nextLine(); 

int i, length, vowels = 0; 
int consonants = 0; 
boolean y = false; 
String j; 
length = phrase.length(); 
for (i = 0; i < length; i++) 
{ 



     j = "" + phrase.charAt(i); 


    boolean isAVowel = "aeiou".contains(j.toLowerCase()); 
    boolean y = "y".contains(j.toLowerCase()); 


    if(isAVowel){ 
     vowels++; 
    }else if(isAVowel && y){ 
    vowels++; 
    consonants++; 
//}else if(y && !(isAVowel)){ 
// vowels++; 
     }else{ 
     consonants++; 
     } 


System.out.println("The number of vowels in \"" +phrase+"\" is "+ vowels+".\n\nThe number of consonants is "+consonants+".\n\n"); 

當「Y」是它本身說,它的輔音,它應該是一個元音。我在哪裏說這個?

+0

順便說一句元音的數量:代替使用子串( i,i + 1),您可以簡單地使用phrase.charAt(i)遍歷字符串中的所有字符。這種方法提高了效率和可讀性。 – mweisz

+0

so like: j = phrase.substring(i); ? – MrAwesome8

+0

其實更像是:j =「」+ phrase.charAt(i); – mweisz

回答

2

有幾件事情會在這裏:

  1. j.equalsIgnoreCase( 「A,E,I,O,U」)將檢查如果j(長度1串)是字符串「a ,e,i,o,u「,這幾乎肯定不是你想要的(因爲它總是假的,因此你爲每個輔音設置y = true)。相反,請考慮在每次迭代開始時將布爾值設置爲false,並在元音分支中將其設置爲true。那麼,如果這個變量是真的,你就知道這次你看到了一個元音。或者只有其他分支。

  2. 您將y初始化爲循環外部的false,但是一旦y爲真,它永遠不會重置,因此對於每個字母,您將運行if(y == true)塊。

  3. 現在,你的系統只能處理1 y和沒有元音的單詞。如果你輸入「yyy」,你會得到1個元音。

從文體上看,還有很多其他更改可以使您的程序更易於閱讀和調試。這裏有幾個:

當你檢查布爾時,你不必做「== true」。例如,不是「if(y == true)」而是「if(y)」。

所有元音的處理方式都是一樣的,所以你不需要爲每個元素分開分支。例如,你可以有:

if (j.equalsIgnoreCase("a") 
    || j.equalsIgnoreCase("e") 
    || j.equalsIgnoreCase("i") 
    || ...) 
{ 
    vowels++; 
} 

事實上,你可以通過檢查對元音值的集合,使用正則表達式,或在這種情況下,進一步簡化這一點,只需使用包含:

boolean isAVowel = "aeiou".contains(j.toLowerCase()); 

考慮單獨計數y,以便在3個單獨的計數器中記錄元音,ys和常量。然後,最後,您可以決定是將y添加到元音還是輔音。

最後,在調試階段,將System.out.println("vowels = " + vowels + ", consonants = " + consonants + "...")添加到循環的內部。這將使它更容易看到發生了什麼以及事情開始出錯的地方。

+0

錯字? '這幾乎可以肯定你想要的' –

1

也許你只需要使用正則表達式

String phrase = in.nextLine(); 
int consonants = phrase.replaceAll("a|e|o|u|i", "").length(); 
int vowels = phrase.replaceAll("[^a|e|o|u|i|y]", "").length(); 
1

我無法理解你想和「Y」,因此已分別計入他們做什麼。 必須首先從輸入中刪除所有非單詞字符。

我已經修改您的代碼(儘管還沒有優化):

System.out.print("Enter a string: "); 
String origphrase = new Scanner(System.in).nextLine(); 
String phrase = origphrase.replaceAll("\\W",""); 
int i, length, vowels = 0; 
int consonants = 0; 
int ys=0; 
String j; 

length = phrase.length(); 
for (i = 0; i < length; i++) 
{ 
    j = "" + phrase.charAt(i); 
    boolean isAVowel = "aeiou".contains(j.toLowerCase()); 
    boolean y = "y".contains(j.toLowerCase()); 
    if(isAVowel){ 
    vowels++; 
    }else if(y){ 
    ys++; 
    }else{ 
    consonants++; 
    } 
} 

System.out.println("Phrase:"+origphrase); 
System.out.println("Vowels:"+vowels); 
System.out.println("Consonants:"+consonants); 
System.out.println("Y's:"+ys); 
+0

在你想要的位置添加'ys'到'元音字母'或'輔音' – SID

0

以下遞歸函數返回輸入字符串

public static int vc(String s){ 
    if(s.length() - 1 < 0) return 0; 
    return ((("aeiou".indexOf((s.charAt(s.length()-1)+"").toLowerCase()) >= 0 ? 1 : 0)) 
    + vc((s = s.substring(0,s.length()-1)))); 
}