在询问用户输入后无限循环

时间:2013-11-02 22:11:45

标签: java loops

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

“实施密码锁类。组合锁具有26个位置标有A ..... Z的表盘。表盘需要设置三次。如果设置正确的组合,锁可以当锁再次关闭时,可以再次进入组合。如果用户将拨盘设置三次以上,则他最后三次设置确定锁是否可以打开。这项练习的一个重要部分是实施CombinationLockclass的接口。“

我已经完成并正确运行了所有内容,但我遇到了部件问题,如果锁定关闭,则可以再次打开组合。我们还没有学过循环,但我记得我的老师曾经提到它们,我认为使用循环可以解决我的问题。我在书中向前看,我尝试使用循环作为我答案的一部分。但是,我认为我最终得到了一个无限循环,我不知道如何解决这个问题。除了使用循环之外,有没有办法做到这一点?

My Tester课程如下:

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

1 个答案:

答案 0 :(得分:0)

程序的布局非常清晰,但在运行时,如果用户输入组合,然后猜测错误,程序就会停止。应该有一个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();

}

}  

唯一需要进一步改进的是,如果用户连续3次错误,通过添加一个int计数器来防止用户再次打开锁定,每次出错都会增加(计数++)然后在

行的正确位置另一个if语句
if(count==3) 
{

    print "no more tries allowed"
    break;

}

else

{

   //do nothing

}

其他部分实际上并不需要,因为其中没有任何内容。