繁体   English   中英

turnRight 方法在密码锁程序中不起作用 - java

[英]turnRight method does not work in Combination Lock program - java

我想编写一个程序来创建一个 3 位密码锁(整数在 0 到 39 之间),当第一个数字向右转,第二个向左转时,它将更新当前位置(从 0 开始),然后右边第三个。 然后它检查是否可以打开锁。 我已经完成了一切,但是当我运行 JUnit 时,测试它假设打开的部分失败了,这是不对的。 任何帮助,将不胜感激。

这是组合锁类:

public class ComboLock {
    private int secret1;
    private int secret2;
    private int secret3;

    private boolean position0 = true;
    private boolean position1, position2, position3 = false;
    private int currentNumber = 0;
    private boolean validSoFar = false;

    /**
     * Initializes the combination of the lock.
     * 
     * @param secret1
     *            first number to turn right to
     * @param secret2
     *            second number to turn left to
     * @param secret3
     *            third number to turn right to
     */
    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() {
        position0 = true;
        position1 = false;
        position2 = false;
        position3 = false;
        validSoFar = true;
    }

    /**
     * Turns lock left given number of ticks.
     * 
     * @param ticks
     *            number of ticks to turn left
     */
    public void turnLeft(int ticks) {
        if (position1 == true) {
            currentNumber = ticks;
            if (currentNumber == secret2) {
                position2 = true;
            } else {
                position2 = false;
            }
        }
    }

    /**
     * Turns lock right given number of ticks
     * 
     * @param ticks
     *            number of ticks to turn right
     */
    public void turnRight(int ticks) {
        if (position0) {
            currentNumber = ticks;
            if (currentNumber == secret1) {
                position1 = true;
                position0 = false;
            }
            if (position1 == true) {
                currentNumber = ticks;
                if (currentNumber == secret3) {
                    position3 = true;
                } else {
                    position3 = false;
                }
            }
        }
    }

    /**
     * Returns true if the lock can be opened now
     * 
     * @return true if lock is in open state
     */
    public boolean open() {
        if (position1 && position2 && position3) {
            validSoFar = true;
        }
        return validSoFar;
    }
}

这些是我的测试用例:

public class ComboLockTester {

    public static void main(String[] args) {
        // Random randomizer = new Random();

        int secret1 = 10;// randomizer.nextInt(40);
        int secret2 = 20;// randomizer.nextInt(40);
        int secret3 = 30;// randomizer.nextInt(40);

        ComboLock lock = new ComboLock(secret1, secret2, secret3);

        Scanner in = new Scanner(System.in);
        boolean opened = false;
        boolean turningRight = true;
        while (!opened) {
            System.out.println("Enter number of ticks to turn to the " + (turningRight ? "right" : "left")
                    + " 0 - 39. Enter an invalid number to quit.");
            int ticks = in.nextInt();
            if ((ticks < 0) || (ticks > 39)) {
                System.out.println("Invalid entry. The program will now exit.");
                return;
            }
            if (turningRight) {
                lock.turnRight(ticks);
                turningRight = !turningRight;
            }

            else {
                lock.turnLeft(ticks);
                turningRight = !turningRight;
            }
            opened = lock.open();
        }
        System.out.println("You opened the lock!");
    }

    @Test
    public void test() {
        ComboLock c = new ComboLock(1, 23, 5);
        c.turnRight(1);
        c.turnLeft(23);
        c.turnRight(5);
        assertTrue("Should open but not", c.open());
    }

    @Test
    public void test2() {
        ComboLock c = new ComboLock(1, 15, 22);
        c.turnRight(1);
        c.turnLeft(15);
        c.turnRight(22);
        assertTrue("Should open but not", c.open());
    }

    @Test
    public void test3() {
        ComboLock c = new ComboLock(1, 3, 7);
        c.turnRight(1);
        c.turnLeft(3);
        c.turnRight(7);
        assertTrue("Should open but not", c.open());
    }

    @Test
    public void test4() {
        ComboLock c = new ComboLock(1, 9, 18);
        c.turnRight(1);
        c.turnLeft(10);
        c.turnRight(18);
        assertFalse("Should not open but did", c.open());
    }

    @Test
    public void test5() {
        ComboLock c = new ComboLock(1, 19, 36);
        c.turnRight(12);
        c.turnLeft(19);
        c.turnRight(36);
        assertFalse("Should not open but did", c.open());
    }
}

编辑:忘了提及,但刻度表示组合的实际价值(基本上是猜测)
EDIT2:格式化更好。

turnRight是错误的,它只考虑position0 它应该考虑position0position2 ,而不是position1

例如...

public void turnRight(int ticks) {
    if (position0) {
        currentNumber = ticks;
        if (currentNumber == secret1) {
            position1 = true;
            position0 = false;
        }
    }
    if (position2 == true) {
        currentNumber = ticks;
        if (currentNumber == secret3) {
            position3 = true;
        } else {
            position3 = false;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM