繁体   English   中英

锁的密码组合不正确,但是由于它最初是打开的,因此保持打开状态

[英]A lock has an incorrect combination but since it was originally open it remains open

我遇到的问题是当锁已经打开时,那么错误的组合几乎不会起作用,因为它将保持打开状态。 当我阅读并尝试它时,这似乎很容易,但是测试用例没有通过。 我标记了我创建的没有注释的代码。 有人可以帮我弄清楚为什么它不起作用吗?

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的工作是保持锁的状态,它是对还是错,所以只需返回它即可。

暂无
暂无

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

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