turnRight方法在组合锁程序-java中不起作用
我想写一个程序,创建一个3位数的组合锁(整数在0和39之间),当第一个数字向右、第二个数字向左、第三个数字向右时,它将更新当前位置(从0开始)。然后检查锁是否可以打开。我已经完成了所有的事情,但是当我运行JUnit测试时,它应该打开的部分失败了,这是不正确的。任何帮助都将不胜感激 下面是combolock类:turnRight方法在组合锁程序-java中不起作用,java,Java,我想写一个程序,创建一个3位数的组合锁(整数在0和39之间),当第一个数字向右、第二个数字向左、第三个数字向右时,它将更新当前位置(从0开始)。然后检查锁是否可以打开。我已经完成了所有的事情,但是当我运行JUnit测试时,它应该打开的部分失败了,这是不正确的。任何帮助都将不胜感激 下面是combolock类: public class ComboLock { private int secret1; private int secret2; private int sec
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());
}
}
公共类组合锁测试仪{
公共静态void main(字符串[]args){
//随机随机发生器=新随机();
int secret1=10;//randomizer.nextInt(40);
int secret2=20;//randomizer.nextInt(40);
int secret3=30;//randomizer.nextInt(40);
组合锁=新的组合锁(secret1、secret2、secret3);
扫描仪输入=新扫描仪(系统输入);
布尔值=假;
布尔右转=真;
当(!打开){
System.out.println(“输入要转到“+(向右旋转?“向右”):“向左”的刻度数)
+“0-39.输入一个无效的退出号码。”);
int ticks=in.nextInt();
如果((刻度<0)| |(刻度>39)){
System.out.println(“无效输入。程序现在将退出”);
返回;
}
如果(右转){
锁。右转(滴答声);
右转=!右转;
}
否则{
锁。左转(滴答声);
右转=!右转;
}
opened=lock.open();
}
System.out.println(“你打开了锁!”);
}
@试验
公开无效测试(){
组合锁c=新的组合锁(1,23,5);
c、 右转(1);
c、 左转(23);
c、 右转(5);
assertTrue(“应打开但不打开”,c.open());
}
@试验
公共无效测试2(){
组合锁c=新的组合锁(1,15,22);
c、 右转(1);
c、 左转(15);
c、 右转(22);
assertTrue(“应打开但不打开”,c.open());
}
@试验
公共无效测试3(){
组合锁c=新的组合锁(1,3,7);
c、 右转(1);
c、 左转(3);
c、 右转(7);
assertTrue(“应打开但不打开”,c.open());
}
@试验
公共无效测试4(){
组合锁c=新的组合锁(1,9,18);
c、 右转(1);
c、 左转(10);
c、 右转(18);
assertFalse(“不应打开,但已打开”,c.open());
}
@试验
公共无效测试5(){
组合锁c=新的组合锁(1,19,36);
c、 右转(12);
c、 左转(19);
c、 右转(36);
assertFalse(“不应打开,但已打开”,c.open());
}
}
编辑:忘记提及,但勾号表示组合的实际值(基本上是猜测)编辑2:格式更好
右转
是错误的,它只考虑位置0。它应该考虑位置0
和位置2
,而不是位置1
例如
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;
}
}
}
如果您要发布代码,请至少努力发布格式良好的代码。@HovercraftFullOfEels感谢您的输入,代码格式正确。
turnRight
是错误的-它只考虑了position0