Java 试图编译代码,但却不断出现相同的错误?

Java 试图编译代码,但却不断出现相同的错误?,java,Java,我不断得到错误:缺少return语句。我的返回声明不是列了5次了吗?有人知道我为什么会得到这个以及如何修复它吗?它指的是从第二个到最后一个括号。如有任何关于发生这种情况原因的帮助/想法,我们将不胜感激。谢谢 public class words { // instance variables - replace the example below with your own private String w; /** *

我不断得到错误:缺少return语句。我的返回声明不是列了5次了吗?有人知道我为什么会得到这个以及如何修复它吗?它指的是从第二个到最后一个括号。如有任何关于发生这种情况原因的帮助/想法,我们将不胜感激。谢谢

public class words
    {
        // instance variables - replace the example below with your own
        private String w;

        /**
         * Default Constructor for objects of class words
         */
        public words()
        {
            // initialise instance variables
            w="";
        }

        /**
         * Assignment constructor
         */
        public words(String assignment)
        {
            w=assignment;
        }

        /**
         * Copy constructor
         */
        public words(words two)
        {
            w=two.w;
        }

        /**
         * Pre: 0<=i<length( )
         * returns true if the character at location i is a vowel (‘a’, ‘e’, ‘i', ‘o’, ‘u’ only), false if not
         */
        private boolean isVowel(int i)
        {
            if (w.charAt(i)=='a')  
            return true; 
            else if (w.charAt(i)=='e')
            return true;
            else if (w.charAt(i)=='i')
            return true;
            else if (w.charAt(i)=='o')
            return true;
            else if (w.charAt(i)=='u')
            return true;
        }

    }

如果你的i不是元音,你就错过了这个例子。

告诉我如果w.charAti是“b”,你会返回什么。 您需要添加最后一行:

private boolean isVowel(int i)
        {
            if (w.charAt(i)=='a')  
            return true; 
            else if (w.charAt(i)=='e')
            return true;
            else if (w.charAt(i)=='i')
            return true;
            else if (w.charAt(i)=='o')
            return true;
            else if (w.charAt(i)=='u')
            return true;
            else return false;
        }
问题

使用括号。没有它们的代码阅读起来很烦人。 在检查具有相同正文的多个if语句时,请使用&&运算符全部返回true 如果您多次比较同一事物w.charAti,但它们有不同的主体,请使用switch语句 这里的实际问题是,如果w.charAti不是元音,那么它什么也不返回。在所有检查之后包括一个返回语句 使用带有元音数组的for循环 注意:我故意不包含代码,因为给出答案没有帮助。如果你不理解上面使用的任何术语,请发表评论或通过谷歌搜索使其完全理解。这将使你从答案中得到最大的收获

private boolean isVowel(int i)
    {
        if (w.charAt(i)=='a')  
        return true; 
        else if (w.charAt(i)=='e')
        return true;
        else if (w.charAt(i)=='i')
        return true;
        else if (w.charAt(i)=='o')
        return true;
        else if (w.charAt(i)=='u')
        return true;
       return false;//Default return statement if nothing has matched.
    }
-您缺少默认返回语句。如果没有找到匹配项,您的metohd将返回什么? -这是问题,我已经在这里更新了你的代码,如果没有发现它将返回false


当其他人解释代码由于缺少return语句而无法编译时,我想指出,您基本上可以作为一个单行程序来完成这项工作,如下所示

private boolean isVowel(int i) {
    return w.charAt(i) == 'a' || w.charAt(i) == e || w.charAt(i) == 'i' || w.charAt(i) == 'o' || w.charAt(i) == 'u';
}

您的代码中缺少返回值。理想情况下,你不应该有那么多的回报

 private boolean isVowel(int i)
{
    boolean found=false;
    if (w.charAt(i)=='a')  
        found= true; 
    else if (w.charAt(i)=='e')
        found= true;
    else if (w.charAt(i)=='i')
        found= true;
    else if (w.charAt(i)=='o')
        found= true;
    else if (w.charAt(i)=='u')
        found= true;

    return found;
}
你有两个选择 1.使用上面这样的标志。您应该使用括号,这使代码易于阅读。
2.在您的代码末尾,只需添加return false。

如果字母不是元音,您会返回什么?@J.Cole,我已经更新了您的代码,并在答案中添加了解释。或者aeiou.indexOfw.charAti!=-1或更惯用的aeiou。包含+w.charAti或数组。如列表'a','e'。沙拉蒂
 private boolean isVowel(int i)
{
    boolean found=false;
    if (w.charAt(i)=='a')  
        found= true; 
    else if (w.charAt(i)=='e')
        found= true;
    else if (w.charAt(i)=='i')
        found= true;
    else if (w.charAt(i)=='o')
        found= true;
    else if (w.charAt(i)=='u')
        found= true;

    return found;
}