gpt4 book ai didi

java - 为什么这个一直循环?

转载 作者:行者123 更新时间:2023-12-02 06:46:35 25 4
gpt4 key购买 nike

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

最佳答案

你确定它正在循环吗?

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

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

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

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

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

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

关于java - 为什么这个一直循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com