如何在java中返回一个布尔方法?

我需要帮助如何在 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()返回一个true或false值.我想这样调用方法

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

如何设置该方法的值?

解决方法

您被允许拥有多个返回语句,因此写入是合法的
if (some_condition) {
  return true;
}
return false;

将布尔值与true或false进行比较也是不必要的,因此可以写入

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;

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...