【问题标题】:How to return a boolean method in java?如何在java中返回一个布尔方法?
【发布时间】:2013-01-04 02:56:29
【问题描述】:

我需要有关如何在 java 中返回布尔方法的帮助。这是示例代码:

public boolean verifyPwd(){
        if (!(pword.equals(pwdRetypePwd.getText()))){
                  txtaError.setEditable(true);
                  txtaError.setText("*Password didn't match!");
                  txtaError.setForeground(Color.red);
                  txtaError.setEditable(false);
           }
        else {
            addNewUser();
        }
        return //what?
}

我希望 verifyPwd() 在我想调用该方法时返回一个值,无论是真还是假。我想这样调用该方法:

if (verifyPwd()==true){
    //do task
}
else {
    //do task
}

如何设置该方法的值?

【问题讨论】:

  • 为什么对一个可能对开始学习的其他人有所帮助的完全有效的问题投反对票?
  • 谁知道你想在什么情况下返回什么?....
  • 由于没有人提到:== true 部分毫无意义。该方法已经返回了boolean,因此if(verifyPwd()) 是完全有效的if 语句。如果仔细选择方法名称以产生像if(passwordRetypeMatches()) 这样的好句子,它也完全可读。
  • 另外,我会避免在名称中只包含验证或匹配的方法中使用太多逻辑:此类逻辑应该在外部完成,很可能在 if-else 分支中完成。

标签: java function methods boolean


【解决方案1】:

return 声明可以有多个,所以写是合法的

if (some_condition) {
  return true;
}
return false;

布尔值也不需要和truefalse比较,所以你可以写

if (verifyPwd())  {
  // do_task
}

编辑:有时你不能早点回来,因为还有更多工作要做。在这种情况下,您可以声明一个布尔变量并在条件块中适当地设置它。

boolean success = true;

if (some_condition) {
  // Handle the condition.
  success = false;
} else if (some_other_condition) {
  // Handle the other condition.
  success = false;
}
if (another_condition) {
  // Handle the third condition.
}

// Do some more critical things.

return success;

【讨论】:

    【解决方案2】:

    试试这个:

    public boolean verifyPwd(){
            if (!(pword.equals(pwdRetypePwd.getText()))){
                      txtaError.setEditable(true);
                      txtaError.setText("*Password didn't match!");
                      txtaError.setForeground(Color.red);
                      txtaError.setEditable(false);
                      return false;
               }
            else {
                return true;
            }
            
    }
    
    if (verifyPwd()==true){
        addNewUser();
    }
    else {
        // passwords do not match
    

    System.out.println("密码不匹配"); }

    【讨论】:

    • 我知道它在他的原始代码中,但是当if 块以return 结尾时,没有理由有else,也没有必要将布尔值与true 进行比较.
    【解决方案3】:
    public boolean verifyPwd(){
            if (!(pword.equals(pwdRetypePwd.getText()))){
                      txtaError.setEditable(true);
                      txtaError.setText("*Password didn't match!");
                      txtaError.setForeground(Color.red);
                      txtaError.setEditable(false);
                      return false;
               }
            else {
                addNewUser();
                return true;
            }
    }
    

    【讨论】:

      【解决方案4】:

      为了便于阅读,您也可以这样做

      boolean passwordVerified=(pword.equals(pwdRetypePwd.getText());
      
      if(!passwordVerified ){
          txtaError.setEditable(true);
          txtaError.setText("*Password didn't match!");
          txtaError.setForeground(Color.red);
          txtaError.setEditable(false);
      }else{
          addNewUser();
      }
      return passwordVerified;
      

      【讨论】:

      • @JayMarz 什么是递归?
      【解决方案5】:

      最好的方法是在代码块中声明Boolean 变量并在代码末尾声明return,如下所示:

      public boolean Test(){
          boolean booleanFlag= true; 
          if (A>B)
          {booleanFlag= true;}
          else 
          {booleanFlag = false;}
          return booleanFlag;
      
      }
      

      我觉得这是最好的方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-03
        • 2013-10-01
        • 1970-01-01
        • 2011-11-28
        • 2016-01-25
        • 2012-08-18
        • 2017-06-08
        相关资源
        最近更新 更多