我是一名参加计算机科学课程的高中生。我们最近被分配了这个项目:
“实现一个密码锁类。一个密码锁有一个26位标有A.....Z的刻度盘。刻度盘需要设置3次。如果设置为正确的密码,则可以打开锁。当锁再次关闭,密码可以再次输入。如果用户设置拨号盘超过 3 次,最后三个设置决定是否可以打开锁。这个练习的一个重要部分是为 CombinationLock 类实现一个合适的接口。
我已经完成并正常运行了所有内容,除了我在锁关闭时可以再次打开密码的部分存在问题。我们还没有了解循环,但我记得我的老师曾经提到过一次,我认为使用循环可以解决我的问题。我在书中向前看,并尝试使用循环作为答案的一部分。但是,我想我最终陷入了无限循环,我不知道如何解决这个问题。除了使用循环之外,还有其他方法可以做到这一点吗?
我的测试人员类如下:
public class CombinationLockTester
{
public static void main(String [] args)
{
System.out.println("*All other questions are answered with Yes or No.");
Scanner kin = new Scanner(System.in);
System.out.print("Enter what you want the combination to be. (All uppercase, with no space seperation) : ");
String userCode = kin.next();
CombinationLock userCombination = new CombinationLock(userCode);
System.out.print("Try to open the lock? : " );
String tryToOpenLockYesNo = kin.next();
if (tryToOpenLockYesNo.equals("Yes"))
{
System.out.print("Enter the lock Combination : " );
String userCombinationLockGuess = kin.next();
userCombination.openLock(userCombinationLockGuess);
if (userCombinationLockGuess.equals(userCode))
{
System.out.print("Correct Combination! Open the Lock? : ");
String openLockYesNo = kin.next();
if (openLockYesNo.equals("Yes"))
{
userCombination.unlock();
System.out.print("The lock is open and you have reaped the rewards of its contents. Will you now close the lock? : ");
String closeLockYesNo = kin.next();
if (closeLockYesNo.equals("Yes"))
{
userCombination.lock();
;
System.out.print("The lock is now closed again. Will you check again to see if you closed it? : ");
String checkLockYesNo = kin.next();
if (checkLockYesNo.equals("Yes"))
{
System.out.println("The lock is locked: " + userCombination.isItClosed());
System.out.print("Re-enter the combination? : " );
String restartYesNo = kin.next();
if (restartYesNo.equals("Yes"))
{
do
{
if (tryToOpenLockYesNo.equals("Yes"))
{
System.out.print("Enter the lock Combination : " );
String userCombinationLockGuess2 = kin.next();
userCombination.openLock(userCombinationLockGuess2);
if (userCombinationLockGuess2.equals(userCode))
{
System.out.print("Correct Combination! Open the Lock? : ");
String openLockYesNo2 = kin.next();
if (openLockYesNo2.equals("Yes"))
{
userCombination.unlock();
System.out.print("The lock is open and you have reaped the rewards of its contents. Will you now close the lock? : ");
String closeLockYesNo2 = kin.next();
if (closeLockYesNo.equals("Yes"))
{
userCombination.lock();
System.out.print("The lock is now closed again. Will you check again to see if you closed it? : ");
String checkLockYesNo2 = kin.next();
if (checkLockYesNo.equals("Yes"))
{
System.out.println("The lock is locked: " + userCombination.isItClosed());
System.out.print("Re-enter the combination? : " );
String restartYesNo2 = kin.next();
if (restartYesNo.equals("Yes"))
{
System.out.println("");
}
else
{
System.out.println("Goodbye." );
}
}
else
{
System.out.println("You did not check to see if the lock is open." );
}
}
else
{
System.out.print("You have left the lock open and unlocked.");
}
}
else
{
System.out.println("The lock is unlocked, but you chose not to open it. Are you afraid to face what's inside?");
}
}
else
{
System.out.println("Incorrect combination!");
}
}
else
{
System.out.println("The secret of the combination lock will always remain a mystery to you...");
}
}
while (userCombination.isItClosed() == true);
}
else
{
System.out.println("Goodbye." );
}
}
else
{
System.out.println("You did not check to see if the lock is open." );
}
}
else
{
System.out.print("You have left the lock open and unlocked.");
}
}
else
{
System.out.println("The lock is unlocked, but you chose not to open it. Are you afraid to face what's inside?");
}
}
else
{
System.out.println("Incorrect combination!");
}
}
else
{
System.out.println("The secret of the combination lock will always remain a mystery to you...");
}
}
课程本身是:
public class CombinationLock
{
private String code;
private String userGuess;
private boolean isLockClosed;
public CombinationLock(String pCode)
{
code = pCode;
isLockClosed = true;
}
public void openLock(String pUserGuess)
{
userGuess = pUserGuess;
if (userGuess.length() > 3)
{
userGuess = userGuess.substring(userGuess.length() - 3, userGuess.length());
}
else
{
userGuess = userGuess;
}
}
public void unlock()
{
if (userGuess.equals(code))
isLockClosed = false;
}
public void lock()
{
isLockClosed = true;
userGuess = "";
}
public boolean isItClosed()
{
return isLockClosed;
}
}