我在几个方法上遇到了多个“错误:丢失返回语句”错误。我做错了什么,我该如何解决?

时间:2014-01-23 03:42:54

标签: java interface compiler-errors

我目前正在尝试修改一个名为Coin的类,该类实现一个名为Lockable的接口来锁定方法,直到输入密码来解锁它们。当我尝试编译我的代码时,我遇到以下错误:

C:\Users\mrowl_000\Dropbox\APCS\Ch. 5\Ch. 5 Projects\Three\Coin.java:112: error: missing return statement
   }
   ^
C:\Users\mrowl_000\Dropbox\APCS\Ch. 5\Ch. 5 Projects\Three\Coin.java:137: error: missing return statement
   }
   ^
2 errors

Tool completed with exit code 1

我的代码如下:

import java.util.Random;

public class Coin implements Lockable
{

   private final int HEADS = 0;
   private final int TAILS = 1;
   private int password;
   private boolean isUnlocked = true;
   private int face;

   public void setKey(int key)
    {
        password = key;
    }

    public void lock(int userKey)
    {
        int userpass = userKey;
        int accessKey = password;

        if (userpass != accessKey)
        {
            System.out.println("The following methods have been locked.");
            isUnlocked = false;
        }
        else
        {
            System.out.println("The following methods have been unlocked.");
            isUnlocked = true;
        }
    }

    public void unlock(int userKey)
    {
        int userpass = userKey;

        if (userpass != password)
        {
            System.out.println("The following methods have been locked.");
            isUnlocked = false;
        }
        else
        {
            System.out.println("The following methods have been unlocked.");
            isUnlocked = true;
        }
    }

    public boolean locked()
    {
        return isUnlocked;
    }

   //END LOCKABLE

   //-----------------------------------------------------------------
   //  Sets up the coin by flipping it initially.
   //-----------------------------------------------------------------
   public Coin ()
   {
      boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");
            }
            else
            {
                flip();
        }
   }

   //-----------------------------------------------------------------
   //  Flips the coin by randomly choosing a face value.
   //-----------------------------------------------------------------
   public void flip ()
   {
       boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");
            }
            else
            {
                face = (int) (Math.random() * 2);
        }
   }

   //-----------------------------------------------------------------
   //  Returns true if the current face of the coin is heads.
   //-----------------------------------------------------------------
   public boolean isHeads ()
   {
      boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");
            }
            else
            {
                return (face == HEADS);
        }
   } // <---Error Here

   //-----------------------------------------------------------------
   //  Returns the current face of the coin as a string.
   //-----------------------------------------------------------------
  public String toString()
   {
      boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");
            }
            else
            {
                String faceName;

                if (face == HEADS)
                   faceName = "Heads";
                else
                   faceName = "Tails";


                return faceName;
        }
   } // <---Error Here
}

如果需要,可以使用名为Lockable的界面:

public interface Lockable
{
   public void setKey (int value);
   public void lock(int key);
   public void unlock(int key);
   public boolean locked();
}

我把评论放在错误发生的地方。有人可以如此友善地指出我哪里出错了吗?

7 个答案:

答案 0 :(得分:2)

两种情况下的IF块都没有返回任何内容,因此错误。所有代码路径都必须返回与返回类型一致的内容,对吗?

答案 1 :(得分:1)

if-else语句的每个分支都需要返回值。以下代码将起作用:

   //-----------------------------------------------------------------
   //  Returns true if the current face of the coin is heads.
   //-----------------------------------------------------------------
   public boolean isHeads ()
   {
      boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");

                return false; // <-- you need a return value here.
            }
            else
            {
                return (face == HEADS);
        }
   } // <---Error Here

   //-----------------------------------------------------------------
   //  Returns the current face of the coin as a string.
   //-----------------------------------------------------------------
  public String toString()
   {
      boolean result = locked();

            if (result == false)
            {
                System.out.println("The coin method is locked");

                return null; // <-- you need a return value here.
            }
            else
            {
                String faceName;

                if (face == HEADS)
                   faceName = "Heads";
                else
                   faceName = "Tails";


                return faceName;
        }
   } 

答案 2 :(得分:0)

该错误非常明确地说明了它出现的原因。

您总是需要在具有返回值的函数中使用return语句。您的isHeads()函数仅在isHeads==true时到达return语句。

例如,如果硬币方法被锁定,您可以抛出异常。然后返回(face == HEADS)

toString()方法相同。

答案 3 :(得分:0)

就像错误所说的那样,你需要在那些行上有一个return语句。

如果在对象被锁定时不想要结果,则应该抛出异常,如下所示:

public boolean isHeads(){
    if(!locked())
        throw new methodLockException();
    return (face == HEADS);
} 

此外,锁定应该反映它所说的内容:如果已锁定,则应该返回true,而不是如果它已解锁,则应该返回,就像现在看来一样。

答案 4 :(得分:0)

即使您有一个return语句,也会收到错误,因为可以在不到达return语句的情况下执行该方法。

public boolean isHeads ()
   {
      boolean result = locked();

            if (result == false)
            {
                **If you enter this if condition, the return statement is never reached**
                System.out.println("The coin method is locked");
            }
            else
            {
                return (face == HEADS);
        }
   } // <---Error Here

您应该设置您的方法,以便在任何情况下返回某些内容

答案 5 :(得分:0)

您需要为toString()isHeads ()添加return语句以删除错误。

答案 6 :(得分:0)

基本上,您没有gurenteed return语句,例如,在以下代码段中...

public boolean isHeads() {
    boolean result = locked();

    if (result == false) {
        System.out.println("The coin method is locked");
        return (face == HEADS);
    }
} // <---Error Here

唯一可能的回报是result == false ...当true时你会返回什么?

您的toString方法也是如此。

就个人而言,我是老上学,我喜欢一个入口点和一个出口点,所以我可能会做类似......

public boolean isHeads() {
    boolean result = locked();

    return !result ? face == HEADS : false;
} // <---Error Here