Подтвердить что ты не робот

Как вернуть логический метод в 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
}

Как установить значение для этого метода?

4b9b3361

Ответ 1

Вам разрешено иметь более одного оператора return, поэтому законно писать

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;

Ответ 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
}

Ответ 3

Вы также можете сделать это для удобства чтения

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;

Ответ 4

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;
        }
}

Ответ 5

Лучшим способом было бы объявить переменную Boolean в блоке кода и return в конце кода, например:

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

}

Я считаю, что это лучший способ.