2013-10-24 77 views
0

我需要編寫一個代碼來返回一個單詞中的元音數,我一直在我的代碼中收到一個錯誤,要求輸入一個缺少的return語句。任何解決方案? :3使用.charAt時丟失返回語句

import java.util.*; 

public class vowels 
{ 
public static void main(String[] args) 
{ 
    Scanner input = new Scanner(System.in); 
    System.out.println("Please type your name."); 
    String name = input.nextLine(); 
    System.out.println("Congratulations, your name has "+ 
         countVowels(name) +" vowels."); 
} 
public static int countVowels(String str) 
{ 
    int count = 0; 
    for (int i=0; i < str.length(); i++) 
    { 
     // char c = str.charAt(i); 
     if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' || str.charAt(i) == 'i' || str.charAt(i) == 'u') 
     count = count + 1; 
    } 
} 
} 
+7

您的countVowels方法是...缺少一個return語句 – Zavior

回答

2

正如幾個註釋所指出的,你錯過了一個return語句。

您需要回到count

public static int countVowels(String str) 
{ 
    int count = 0; 
    for (int i=0; i < str.length(); i++) 
    { 
     // char c = str.charAt(i); 
     if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'o' || 
      str.charAt(i) == 'i' || str.charAt(i) == 'u') 
     count = count + 1; 
    } 

    return count; 
}