gpt4 book ai didi

Java密码锁

转载 作者:行者123 更新时间:2023-12-01 22:07:10 24 4
gpt4 key购买 nike

因此,该程序的目的是创建一个 3 位密码锁(整数介于 0 到 39 之间),它将表盘的当前位置(从 0 开始)更新为向右、向左、向右,然后向左。如果发现所有位置均为 true,则 ComboLock 将解锁。我的代码的问题是,当我运行程序并在右侧输入正确的刻度数来更改第一个位置的值时,它只是说组合错误而不是提示我到位置2。这是我的到目前为止我的 ComboLock 类的代码:

public class ComboLock
{

private int currentNumber = 0; //current value lock dial is set to

private int secret1, secret2, secret3;
private boolean pos0 = true;
private boolean pos1, pos2, pos3 = false;
private boolean unlock = false;

public ComboLock(int secret1, int secret2, int secret3)
{
this.secret1 = secret1;
this.secret2 = secret2;
this.secret3 = secret3;
}
/**
Resets the state of the lock so that it can be opened again.
*/

public void reset()
{
pos0 = true;
pos1= false;
pos2 = false;
pos3 = false;
}


public void turnLeft(int ticks)
{
if(pos1 == true)
{
currentNumber += ticks;
if(currentNumber == secret2)
{
pos2 = true;
}
else
{
pos2 = false;
}

}


}

public void turnRight(int ticks)
{
if(pos0)
{
currentNumber = (40 - ticks);

if(currentNumber == secret1)
{
pos1 = true;
}
}
else
{
if(currentNumber - ticks > 0)
{
pos3 = true;
}
else
{
currentNumber = (40 - (ticks - currentNumber));
pos3 = false;
if(currentNumber == secret3)
{
pos3 = true;
}
}

}

}

public boolean open()
{
if(pos1 && pos2 && pos3)
{
unlock = true;
System.out.println("Click!");
}
else
{
unlock = false;
System.out.println("Wrong! Lets try again.");
}
return unlock;
}

public int getCurrentNumber()
{
return currentNumber;
}

}

最佳答案

撇开设计不谈,只有一些小错误。

当你左转时,你不能简单地添加 ticks给您currentNumber因为数字只能在0-39之间但您的代码允许大于 39 。所以需要使用模运算符%环绕 40 个数字。

turnLeft

//          currentNumber += ticks;
// Should be
currentNumber = (currentNumber + ticks)%40;

下一个问题是你永远不会从 pos0 继续前进当右转时,在您的代码中,您永远不会移动到 else turnRight的一部分方法。

public void turnRight(int ticks) {
// ... your code ... //
if (currentNumber == secret1) {
pos1 = true;
pos0 = false; // Add this
}
} // ... your code ... //
}

编辑:这应该可以解决您的问题。但是代码相当难以维护,特别是当您开始增加组合锁的大小时。为了解决这个问题并解决 jchamp 提到的问题,我认为我已经完全重构了该类,使其更短、更灵活:

public class ComboLock {

private static final int MAX_NUMBERS = 40;

private int currentNumber = 0; // current value lock dial is set to
private int combination[] = null; // holds the combination to the lock
private int currentPosition = 0; // current position of the combination array used for comparison

// Allow for a lock that can handle more than size 3
public ComboLock(int ... combination) {
this.combination = combination;
}

/**
* Resets the state of the lock so that it can be opened again.
*/
public void reset() {
currentPosition = 0;
}

public void turnLeft(int ticks) {
currentNumber = (currentNumber + ticks) % MAX_NUMBERS;
// Only compare the number when turning left the current position is odd
if (currentPosition%2 == 1 && combination[currentPosition] == currentNumber) {
currentPosition = Math.min(currentPosition + 1, combination.length - 1);
}
}

public void turnRight(int ticks) {
currentNumber = (currentNumber + (MAX_NUMBERS - ticks % MAX_NUMBERS)) % MAX_NUMBERS;
// Only compare the number when turning right and the current position is even
if (currentPosition%2 == 0 && combination[currentPosition] == currentNumber) {
currentPosition = Math.min(currentPosition + 1, combination.length - 1);
}
}

public boolean open() {
return combination[currentPosition] == combination[combination.length - 1];
}

public int getCurrentNumber() {
return currentNumber;
}

public static void main(String[] args) {
ComboLock combo = new ComboLock(39, 25, 35);
combo.turnRight(1);
combo.turnLeft(26);
combo.turnRight(30);
assert combo.open();
combo = new ComboLock(39, 25, 35);
combo.turnLeft(39);
combo.turnRight(14);
combo.turnLeft(40);
assert !combo.open();
}
}

关于Java密码锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32491189/

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