0

当我尝试运行我的程序时遇到的错误是,它在我第一次完全完成所有 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,最后由于某种原因在几个循环后调用了 turnThird。这是 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; 
  }
}
4

1 回答 1

2

你确定它是循环的?

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

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

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

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

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

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

于 2013-09-03T14:49:11.027 回答