为什么这会循环?

时间:2013-09-02 17:22:10

标签: java loops

我尝试运行程序时遇到的错误是,在我第一次完全完成所有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;
}

private boolean turnFirstNumber()
{
  revoCount = 0;
  System.out.print("11111111What 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("\n11111111111What is your desired first 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)));
  }

  tempFirst = activeNumber;

  if ((activeNumber == first) && (count < 0)) //if first number is correct and user picked correct orientation and revolutions
    return true;
  else
    return false;
}

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

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

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

1 个答案:

答案 0 :(得分:2)

你确定它在循环吗?

在你的openLock方法中,它在每个if语句中调用方法 turnFirstNumber turnSecondNumber turnThirdNumber

如果最后一个数字不正确,它会调用方法 turnFirstNumber turnSecondNumber turnThirdNumber 5次。

我认为最好引入 firstTurnCorrect secondTurnCorrect thirdTurnCorrect 等变量,并比较这些值以获得正确的消息:

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

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

这种方法只会被调用一次。