&&逻辑运算符不起作用,但||操作员确实

时间:2018-12-22 06:30:42

标签: java javafx logical-operators

我的&&逻辑运算符将不起作用,并且始终返回false并将第二个条件返回为true。但这对于||确实有效操作员。我在做什么错了?

我尝试使用||运算符,它工作正常,但我想使用&&运算符

@FXML
private void loginFunction() throws IOException {

    String username = this.username.getText();
    String password = this.username.getText();

    if (username.equals("username") && password.equals("password")) {

        // This Code is used to alert the user of any information in case the username
        // and password is all correct
        Alert alert2 = new Alert(Alert.AlertType.CONFIRMATION);
        alert2.setHeaderText("Login Successful");
        alert2.setTitle("Welcome");
        alert2.setContentText("Pleae Wait. You shall be redirected soon.");
        alert2.show();
    } else {
        Alert alert3 = new Alert(Alert.AlertType.WARNING);
        alert3.setTitle("Intruder Found");
        alert3.setContentText("Either your username or password is incorrect. Please try again.");
        alert3.setHeaderText("Enter the Right Details");
        alert3.show();
        return;
    }
}

我希望第一个条件返回为true,但事实并非如此。 ||并非如此运算符。

2 个答案:

答案 0 :(得分:6)

好吧

String username = this.username.getText();
String password = this. username.getText();

表示您的两个字段相同。更改它,

String username = this.username.getText();
String password = this.password.getText();

因为username中的文本不能同时同时“用户名”和“密码”。

答案 1 :(得分:0)

更改

String password = this.username.getText();

String password = this.password.getText();