&&逻辑运算符不起作用,但||运营商做了[关闭]

问题描述 投票:-4回答:2

我的&&逻辑运算符将无法工作,并始终返回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;
    }
}

我希望第一个条件返回为真,但事实并非如此。对于||,情况并非如此运营商。

java javafx logical-operators
2个回答
6
投票

好吧

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

意味着你的两个领域是相同的。改变它,像,

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

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


0
投票

更改

String password = this.username.getText();

如,

String password = this.password.getText();
© www.soinside.com 2019 - 2024. All rights reserved.