我目前正在尝试修改名为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();
}
我在错误发生的地方加了注释。有人会这么友善地指出我哪里出问题了吗?
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] 删除。
我来说两句