Java组合锁程序

Java组合锁程序,java,input,Java,Input,我对Java真的很陌生,我对Java类所需的程序有问题。我需要模拟一个锁,实现改变组合的方法,检查顶部的数字等等 嗯,我认为我的openLock()方法或alterlockcomposition()方法有问题。我的程序将使用默认组合(0,0,0)正确打开锁,但当我尝试更改组合时,它将无法正常工作,只有默认组合才能解锁锁 我的错误在哪里 import java.util.Scanner; public class Lock { public static final int CLOCKWISE

我对Java真的很陌生,我对Java类所需的程序有问题。我需要模拟一个锁,实现改变组合的方法,检查顶部的数字等等

嗯,我认为我的
openLock()
方法或
alterlockcomposition()
方法有问题。我的程序将使用默认组合(0,0,0)正确打开锁,但当我尝试更改组合时,它将无法正常工作,只有默认组合才能解锁锁

我的错误在哪里

import java.util.Scanner;

public class Lock {

public static final int CLOCKWISE = 0;
public static final int COUNTER_CLOCKWISE = 1;

Scanner in = new Scanner(System.in);

private int x;
private int y;
private int z;
private boolean isLockOpen;
private int noOnTopOfKnob;

public Lock() {
    x = 0;
    y = 0;
    z = 0;
    this.isLockOpen = false;
    this.noOnTopOfKnob = 0;
}

public void alterLockCombinaiton(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
}

public void turnKnob(int direction, int noToStop){
    int i = noOnTopOfKnob;
    int numbersPassed = 0;
    System.out.println("Simulating......");

    do{
        if(direction == CLOCKWISE)
            i++;
        else if(direction == COUNTER_CLOCKWISE)
            i--;
        if(i > 39)
            i = 0;
        if (i < 0)
            i=39;
        this.noOnTopOfKnob = i;

        System.out.print(noOnTopOfKnob + " ");
        numbersPassed++;

        if(numbersPassed>40 && noOnTopOfKnob==noToStop)
            break;
    }
    while(true);
    System.out.println();
}

public void closeLock() {
    System.out.println("Locked!");
    this.isLockOpen = false;
}

public boolean openLock() {
    // initializing with arbitrary values
    int firstStop = -1;
    int secondStop = -1;
    int thirdStop = -1;
    int firstRotation = -1;
    int secondRotation = -1;
    int thirdRotation = -1;

    for(int i = 1; i <= 3; i++){
        System.out.print("Enter a number (0-39) " + i + ": ");
        int noToStop = in.nextInt();
        System.out.print("Enter 0 for clockwise and 1 for counter-clockwise) " + i + ": ");
        int direction = in.nextInt();
        turnKnob(direction, noToStop);

        if(i == 1) {
            firstStop = noToStop;
            firstRotation = direction;

        }
        else if(i == 2) {
            secondStop = noToStop;
            secondRotation = direction;
        }
        else if(i == 3) {
            thirdStop = noToStop;
            thirdRotation = direction;
        }

        if(firstStop == this.x && firstRotation == CLOCKWISE
                && secondStop == this.y && secondRotation == COUNTER_CLOCKWISE
                && thirdStop == this.z && thirdRotation == CLOCKWISE) {

            this.isLockOpen = true;
        }

    }
    return isLockOpen;
}

public boolean isLockOpen() {
    return this.isLockOpen;
}

public int getNoAtTop() {
    return noOnTopOfKnob;
}

}

LockInput类未完成,因为我正在尝试首先解决输入问题。

问题是每次调用
菜单()
时,您都在创建一个
新锁()

因此,修改后的组合将立即替换为默认组合,因为在此之后将调用
menu()
,并将锁替换为新的
lock
实例:

newLock.alterLockCombinaiton(xInput,yInput,zInput);
menu();
您可能需要删除
Lock newLock=new Lock()菜单()
,将其声明为类级别的静态变量:

static Lock newLock = new Lock();
更好的是,@GhostCat建议避免使用静态变量:

main
方法中创建对象

Lock newLock = new Lock();
菜单()
方法更改为
菜单(Lock newLock)

然后从
main
菜单调用它(newLock)


对于
Scanner
变量也是如此,您可能会计算出该部分。

只是为了记录:不要使用“int”来定义时钟/反时钟。使用枚举更合适……除此之外:在垂直间距方面更严格:A)遵循java编码标准,确定放大括号/换行符的位置B)保持一致:不要随意放新行:使用它们将属于一起的内容真正分组。。。在一起不要只使用新线,因为你可以!我建议main()创建Scanner/Lock并将其作为参数传递给menu()。非常感谢!我在main中创建了对象和扫描器,并将它们作为参数传递给menu(),这就成功了。
Lock newLock = new Lock();