首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java组合锁

Java组合锁
EN

Stack Overflow用户
提问于 2015-09-10 00:31:39
回答 1查看 9.8K关注 0票数 0

因此,该程序的目的是创建一个3位组合锁(整数介于0到39之间),该锁将把刻度盘的当前位置(从0开始)更新到右边,然后向左,然后向右,然后left.If所有位置都为真,然后ComboLock将解锁。我的代码的问题是,当我运行程序并向右输入正确的滴答数以更改第一个位置的值时,它只是说组合错误,而不是促使我定位2。到目前为止,我的ComboLock类代码如下:

代码语言:javascript
复制
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;
 }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-10 02:13:05

撇开设计不说,只是几个小错误。

当您向左转时,您不能简单地将ticks添加到您的currentNumber中,因为这个数字只能在0-39之间使用,但是您的代码允许大于39。因此,您需要使用模运算符%来包装大约40个数字。

turnLeft

代码语言:javascript
复制
//          currentNumber += ticks;
            // Should be
            currentNumber = (currentNumber + ticks)%40;

下一个问题是,在向右转的时候,您永远不会从pos0中移动,因此在代码中,您永远不会移动到turnRight方法的else部分。

代码语言:javascript
复制
public void turnRight(int ticks) {
    // ... your code ... //
        if (currentNumber == secret1) {
            pos1 = true;
            pos0 = false; // Add this
        }
    } // ... your code ... //
}

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

代码语言:javascript
复制
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();
    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32491189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档