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

mrowland2

我目前正在尝试修改名为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();
}

我在错误发生的地方加了注释。有人会这么友善地指出我哪里出问题了吗?

Weibo Li

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

什么是错误?我该如何解决?

我在 mysql pdo 上遇到了一些奇怪的错误。我该如何解决?

为什么我在初始化字母时遇到错误,该如何解决?

我该如何解决此缺失的return语句错误?

我该如何解决这个错误?开关语句

我做错了什么导致分段错误?

什么是异常错误,我该如何解决?

我该如何解决细分错误?

我试图用discord.py构建一个歌词命令,我得到这个错误。关于我做错了的任何想法,我该如何解决?

杰克逊(Jackson)的mixin类无法解决问题:错误或我做错了什么?

语法错误:参数列表后缺少“)” - 我该如何解决?

我该如何解决我的构建错误?

我的代码导致STARTTLS错误,我该如何解决?

我想在回调中添加多个参数。我该如何解决这个错误?

我该如何解决该错误?[覆盖/ libgstreamer]

我在哈希数组上尝试使用sort_by方法时收到错误消息。我该如何解决?

我该如何解决此逻辑错误?代码有什么错误?

为什么我得到了错误的输出,我该如何解决这个问题?

这个错误试图告诉我什么,我该如何解决?

是什么导致我的代码出现分段错误,我该如何解决?

为什么在我运行脚本时出现错误。我该如何解决?

为什么我收到属性错误?我该如何解决?

令牌“.”上的语法错误,预期:这是什么意思,我做错了什么?

我遇到这些错误,不知道如何解决

我在使用float函数时遇到错误,如何解决?

我如何解决“处理时遇到错误:mmodemanager whoopsie

程序返回错误答案和“-nan(ind)”。我做错了什么?

这个 if 语句我做错了什么?

是MSVC 2010中的错误,还是我做错了什么?