Java 为什么会不断循环?

Java 为什么会不断循环?,java,loops,Java,Loops,当我尝试运行我的程序时,我遇到的错误是,在我第一次完全通过所有3个“圈”后,它在turnFirstNumber()和turnSecondNumber()之间不断循环 编辑:请参见底部 我的测试班: public class testLock { public static void main (String[] args) { Lock testLock = new Lock(); testLock.turnLock(); retu

当我尝试运行我的程序时,我遇到的错误是,在我第一次完全通过所有3个“圈”后,它在
turnFirstNumber()
turnSecondNumber()
之间不断循环

编辑:请参见底部

我的测试班:

public class testLock
{
    public static void main (String[] args)
    {
        Lock testLock = new Lock();
        testLock.turnLock();
        return;
    }
}
以下是我的代码片段,它们让我感到悲伤:

public void turnLock()
{       
  System.out.print("This is a lock that goes from 0 to 39. You must turn the knob clockwise first, then counterclockwise twice, ");
  System.out.print("then clockwise for the final input. Specify how many revolutions you want (Positive number indicates ");
  System.out.println("COUNTER CLOCKWISE. Negative number indicates CLOCKWISE.");

  turnFirstNumber();
  turnSecondNumber();
  turnThirdNumber();

    System.out.println("The combination you chose was: " + tempFirst + ", " + tempSecond + ", and " + tempThird + ".");
  return;
}

编辑:所以在更仔细地测试之后,我发现我的
openLock()
方法可能以某种方式调用了我的turnFirst、turnSecond和turnThird方法。我在测试类中注释掉了我的turnLock()方法,然后运行openLock()方法,它开始多次调用turnFirst和turnSecond,最后由于某种原因在几个循环后调用turnsecord。这是openLock()


你确定它在循环吗

在openLock方法中,它在每个if语句中调用方法turnFirstNumberturnSecondNumberturnThirdNumber

如果最后一个数字不正确,它将调用方法turnFirstNumberturnSecondNumberturnThirdNumber各5次

我认为最好引入变量,如firstTurnCorrectsecondTurnCorrectthirdTurnCorrect,并比较这些值以获得正确的信息:

boolean firstTurnCorrect = turnFirstNumber();
boolean secondTurnCorrect = turnSecondNumber();
boolean thirdTurnCorrect = turnThirdNumber();

if (!firstTurnCorrect && secondTurnCorrect && thirdTurnCorrect) ...

这样,这些方法只调用一次。

在我看来,有很多不必要的代码。只需进一步缩小问题的范围,并发布一个只与问题相关的代码。您是否尝试在调试器下运行它?您的代码风格非常糟糕。我建议您将输入与逻辑分开。为此,向方法添加参数,如
private boolean turnFirstNumber(int count)
,并使用常量而不是硬编码数字(例如
public static final int MAX_CIPHER=40
)+添加注释,所以其他人可以理解你的想法。我认为在你按照@mike的建议重新编写之前,这仍然很难调试——看看
turnXNumber
方法有多相似?在重新组织代码的过程中,您可能会发现您已删除或开始理解问题。编程的本质是抽象。因此,我编写openLock()方法的方式,不是检查turnFirstNumber()、turnSecondNumber()和turnThirdNumber()的结果是否为真/假,而是运行每个方法,然后检查它们是否为真/假?那绝对是问题所在。
private boolean turnSecondNumber()
{
  revoCount = 0;
  System.out.print("2222222222What is your desired direction and number of revolutions? (Positive number is counterclockwise, negative number is clockwise): ");
  count = in.nextInt();
  if (count > 0)
    isClockwise = false;
  else if (count < 0)
    isClockwise = true;
  else
  {
    throw new IllegalArgumentException("Your desired direction of spinning the lock is invalid. Please choose a number other than 0.");
  }
  System.out.print("\n222222222What is your desired second number?: ");
  desiredNumber = in.nextInt();

  if (!isClockwise) //user desires countercockwise revolution
  {
    do {
      for (int i = 0; i < (count * 40); i++)
      {
        activeNumber++;
          if (activeNumber > 39)
            activeNumber = 0;
          if (activeNumber == desiredNumber)
            revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < count));
  }      
  else if (isClockwise) //user desires clockwise revolution
  {
    do {
      for (int i = 0; i < (Math.abs(count) * 40); i++)
      {
      activeNumber--;
        if (activeNumber < 0)
          activeNumber = 39;
        if (activeNumber == desiredNumber)
          revoCount++;
      }
       } while ((activeNumber != desiredNumber) && (revoCount < Math.abs(count)));
  }

  tempSecond = activeNumber;

  if ((activeNumber == second) && (count == 2)) //if second number is correct and user picked correct orientation and revolutions
    return true;
  else
    return false;
}
private boolean turnThirdNumber()
{
  revoCount = 0;
  System.out.print("Enter '1' to twist the dial counterclockwise until you reach your desired number. Enter '-1' to twist the dial clockwise until you reach your desired number.: ");
  count = in.nextInt();
  if (count == 1)
    isClockwise = false;
  else if (count == (-1))
    isClockwise = true;
  else
  {
    throw new IllegalArgumentException("You are not supposed to do a full revolution on the third number of the combination. Now you have to restart.");
  }
  System.out.print("\n333333333What is your desired third and final number?: ");
  activeNumber = in.nextInt();
  activeNumber = Math.abs(activeNumber);
  tempThird = activeNumber;

  if (activeNumber > 39)
  {
    throw new IllegalArgumentException("You desire a number that is not on the lock. The lock goes from 0 to 39. Try again.");
  }
  if ((activeNumber == third) && (isClockwise)) //if third number is correct and user picked correct orientation and revolutions
    return true;
  else    
    return false;
}
public void openLock()
{ 
  if ((turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber()) && (isClosed)) //if all 3 passed and lock is not open already
  { 
    isClosed = false;
    System.out.println("Your combination is correct and the lock has been opened.");
    return; 
  }
  else if (!isClosed)                                                        //lock's already open
  { 
    System.out.println("The lock is already open.");
    return;
  }
  else if ((!turnFirstNumber()) && (turnSecondNumber()) && (turnThirdNumber())) //first wrong
  {
    System.out.println("The first number you input is incorrect.");
    return;
  }
  else if ((!turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //first and second wrong
  {
    System.out.println("The first 2 numbers you input are incorrect.");
    return;
  }
  else if ((!turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //first and third wrong
  {
    System.out.println("The first and last numbers you input are incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (turnSecondNumber()) && (!turnThirdNumber())) //third wrong
  {
    System.out.println("The last number you input is incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (!turnSecondNumber()) && (!turnThirdNumber())) //second and third wrong
  {
    System.out.println("The second and last numbers you input are incorrect.");
    return;
  }
  else if ((turnFirstNumber()) && (!turnSecondNumber()) && (turnThirdNumber())) //second is wrong
  {
    System.out.println("The second number you input is incorrect.");
    return;
  }
  else
  { 
    System.out.println("Your entire combination is INCORRECT. Please try again."); //all wrong
    return; 
  }
}
boolean firstTurnCorrect = turnFirstNumber();
boolean secondTurnCorrect = turnSecondNumber();
boolean thirdTurnCorrect = turnThirdNumber();

if (!firstTurnCorrect && secondTurnCorrect && thirdTurnCorrect) ...