Java 锁的组合不正确,但由于它最初是打开的,所以它保持打开状态

Java 锁的组合不正确,但由于它最初是打开的,所以它保持打开状态,java,methods,locking,Java,Methods,Locking,我遇到的问题是,当一个锁已经打开时,几乎不正确的组合不会产生任何影响,因为它将保持打开状态。当我阅读并尝试它时,这看起来很容易,但是测试用例没有通过。我标记了我创建的代码中没有注释的部分。有人能帮我弄清楚为什么它不起作用吗 public void open(Combination opening){ Lock temp = new Lock(upper, opening); if(opening.equals(unlock)){ cl = true;

我遇到的问题是,当一个锁已经打开时,几乎不正确的组合不会产生任何影响,因为它将保持打开状态。当我阅读并尝试它时,这看起来很容易,但是测试用例没有通过。我标记了我创建的代码中没有注释的部分。有人能帮我弄清楚为什么它不起作用吗

public void open(Combination opening){
    Lock temp = new Lock(upper, opening);

    if(opening.equals(unlock)){
         cl = true;

    }else {
    //this if statement is what I came up with to find if it is open 
        if(temp.isOpen() == true){
            cl = true;
        }
        cl = false;

    }
}


public boolean isOpen() {

    boolean op = true;
    if(cl == false){
        op = false;
    }   

    return op;
}


public void close() {

        cl = false;

}

这里有几个风格问题,但我认为问题可能在于你的临时锁

if(temp.isOpen() == true){
我不明白你为什么需要临时锁

public void open(Combination opening){

   // If the combination is right open the lock
   // if it was already open no change
   if(opening.equals(unlock)){
     opcl = true;
   } 
   // no else, if combination was wrong 
   // leave the status as it was
}
现在作为一个文体问题,你对待布尔人的方式非常糟糕。从不写信

if ( bvalue == true )
只要写

if ( bvlaue )
这就是布尔人的全部观点,他们是对的还是错的

因此,您的支票远比需要的复杂,这就是您所需要的全部

// The  method  isOpen, which   returns a   
// boolean indicating  whether the lock    is  opened  or  not.
public boolean isOpen() {   
    return opcl;
}
opcl的任务是保持锁的状态,不管是真是假,所以只需返回该状态即可