1

我是一名参加计算机科学课程的高中生。我们最近被分配了这个项目:

“实现一个密码锁类。一个密码锁有一个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;
}
}
4

1 回答 1

0

你的程序布局很清晰,但是在运行时,如果用户输入了组合,然后猜错了,程序就会停止。应该有一个比您拥有的更高的 do while 循环,以便在所描述的事件中,询问用户是否要进行另一个猜测。

程序应该什么时候结束呢?当他们做出 3 次错误猜测时,或者当用户说他们希望它结束​​时,或两者兼而有之?需要回答这些问题,并在必要时对程序进行相应修改。不过看起来不错。走到这一步,你应该能够让它发挥作用。

另外,请记住,您可以使用

break;  

如果需要,可以跳出循环。

完成后,您还应该关闭扫描仪,如下所示:

kin.close();

对您的 CombinationLockTester 类进行以下一些修改会产生所需的结果:

import java.util.Scanner;

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

    do
    {

    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"))
                        {

                                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." );
                                                        break;
                                                    }
                                                }
                                                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...");
                                    break;
                                }

                        }
                        else
                        { 
                            System.out.println("Goodbye." );
                            break;
                        }
                    }
                    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...");
        break;
    }

    }

    while (userCombination.isItClosed() == true);

    kin.close();

}

}  

唯一需要的其他改进是通过添加一个 int 计数器来防止用户在连续 3 次出错时继续打开锁,该计数器在每次出错时递增(count++),然后如果沿线的正确位置声明

if(count==3) 
{

    print "no more tries allowed"
    break;

}

else

{

   //do nothing

}

else 部分实际上并不需要,因为它没有做任何事情。

于 2013-11-02T23:00:05.757 回答