Java 请求用户输入后的无限循环
我是一名高中生,选修了计算机科学课程。我们最近被分配了这个项目: “实现一个组合锁类。组合锁有一个刻度盘,其26个位置标记为A…..Z。刻度盘需要设置三次。如果将其设置为正确的组合,则可以打开锁。当锁再次关闭时,可以再次输入组合。如果用户拨盘超过三次,最后三次设置决定锁是否可以打开。此练习的一个重要部分是为CombinationLockclass实现一个合适的接口。” 我已经完成了所有的工作,并且运行正常,除了我遇到了一个问题,那就是如果锁被关闭,组合可以再次打开。我们还没有学习循环,但我记得我的老师曾经提到过它们,我认为使用循环可以解决我的问题。我在书中展望了未来,我发现ed建议我使用循环作为我答案的一部分。但是,我认为我最终得到了一个无限循环,我不知道如何解决这个问题。除了使用循环,还有其他方法可以做到这一点吗 我的测试仪等级如下:Java 请求用户输入后的无限循环,java,loops,Java,Loops,我是一名高中生,选修了计算机科学课程。我们最近被分配了这个项目: “实现一个组合锁类。组合锁有一个刻度盘,其26个位置标记为A…..Z。刻度盘需要设置三次。如果将其设置为正确的组合,则可以打开锁。当锁再次关闭时,可以再次输入组合。如果用户拨盘超过三次,最后三次设置决定锁是否可以打开。此练习的一个重要部分是为CombinationLockclass实现一个合适的接口。” 我已经完成了所有的工作,并且运行正常,除了我遇到了一个问题,那就是如果锁被关闭,组合可以再次打开。我们还没有学习循环,但我记得我
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;
}
}
程序的布局非常清晰,但在运行时,如果用户输入组合,然后猜测哪一个是错误的,则程序停止。应该有一个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计数器(每次出错都会递增)(count++),然后在代码行的正确位置添加另一个if语句,防止用户在连续3次出错时再次打开锁
if(count==3)
{
print "no more tries allowed"
break;
}
else
{
//do nothing
}
实际上不需要else部分,因为其中没有任何操作。注意,
isItClosed()
返回一个boolean
,因此usercomposition.isItClosed()==true
是冗余的;while(usercomposition.isItClosed())
是首选。我想不出一种没有循环的方法来实现这一点,但如果你还没有了解循环,那么设置这个似乎很奇怪。在程序中任何有if/else或do的地方都放上print语句,以确保它做了它应该做的事情(事实并非如此)应该揭示出错误所在。+1表示可编译代码。考虑到它是相同的,整个第二次猜测可以汇总为一个do。你是说程序中只需要一个do循环吗?是的,第二部分是第一次用户交互的精确副本、提示和所有内容。是的,我想是这样。+1表示有用的信息。