因此,该程序的目的是创建一个3位数的密码锁(整数在0到39之间),该锁将使拨盘的当前位置(从0开始)向右,向左,向右,然后向右更新。如果所有位置都正确,那么ComboLock将解锁。我的代码的问题是,当我运行程序并在右侧输入正确的跳动数以更改第一个头寸的值时,它只是说组合是错误的,而不是提示我到第二个位置。这是我的到目前为止,我的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 - Java密码锁,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32491189/