Reputation: 11
The error I'm getting when I try to run my program is that it keeps looping between turnFirstNumber()
and turnSecondNumber()
after I go through all 3 'turns' entirely the first time.
EDIT: SEE BOTTOM.
My Test Class:
public class testLock
{
public static void main (String[] args)
{
Lock testLock = new Lock();
testLock.turnLock();
return;
}
}
Here's my code segments causing me grief:
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;
}
EDIT: So after testing more carefully, I found that my openLock()
method may be calling my turnFirst, turnSecond, and turnThird methods somehow. I commented out my turnLock() method in my test class and ran the openLock() method and it started calling turnFirst and turnSecond multiple times and finally turnThird for some reason after a few loops. Here's the 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;
}
}
Upvotes: 0
Views: 566
Reputation: 1552
Are you sure it's looping?
In your openLock method it's calling the methods turnFirstNumber, turnSecondNumber and turnThirdNumber in every if-statement.
In case the last number is incorrect, it has called the methods turnFirstNumber, turnSecondNumber and turnThirdNumber each 5 times.
I think it's better to introduce variables like firstTurnCorrect, secondTurnCorrect and thirdTurnCorrect and compare those values to get the right message:
boolean firstTurnCorrect = turnFirstNumber();
boolean secondTurnCorrect = turnSecondNumber();
boolean thirdTurnCorrect = turnThirdNumber();
if (!firstTurnCorrect && secondTurnCorrect && thirdTurnCorrect) ...
This way those methods only gets called once.
Upvotes: 2