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

时间:2018-10-24 19:01:05

标签: java

我不断收到错误:“缺少返回语句”。我的退货单不是列出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;
        }

    }

6 个答案:

答案 0 :(得分:1)

告诉我,如果w.charAt(i)为'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;
        }

答案 1 :(得分:0)

private boolean isVowel(int i){
   //...
   return false;
}

您错过了我不是元音的情况。

答案 2 :(得分:0)

问题

  1. 使用方括号。没有它们的代码很烦人。
  2. 在检查具有相同主体的多个if语句时,请使用&&运算符(都返回true)
  3. 如果多次比较同一事物(w.charAt(i))但它们具有不同的主体,请使用switch语句
  4. 您这里遇到的实际问题是,如果w.charAt(i)不是元音,那么它什么也不会返回。在所有检查之后添加退货声明
  5. 使用带元音数组的for循环

(注意:我故意不包含代码,因为这对给您答案没有帮助。如果您不理解上面使用的任何术语,请评论或用谷歌搜索它们以完全理解。这将使您获得最无法回答的问题。)

答案 3 :(得分:0)

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.
    }
  

-您缺少默认的return语句。如果找不到匹配项,您的方法将返回什么?   -是问题,我在这里更新了您的代码,如果找不到任何内容,它将返回false。

答案 4 :(得分:0)

虽然其他人正在解释由于缺少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';
}

答案 5 :(得分:0)

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

 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