即使条件正确,if条件也不会执行 [英] If-condition never executes despite correct condition

查看:60
本文介绍了即使条件正确,if条件也不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段简单的代码可以验证用户名和密码.

I have a simple piece of code to validate a username and a password.

    public boolean isValid(String u, String p) {
    if (u=="admin" && p=="password1") {
        return true;
    } else if (u=="user" && p=="password2") {
        return true;
    } else {
        return false;
    }

}

我尝试调试它,当它运行时,u的值为"admin",p的值为"password1",但它只是跳过了第一个条件.我一定做错了什么,但我不知道该怎么办.

I've tried debugging it, and when it runs, u has the value "admin" and p has the value "password1", but it just skips the first condition. I must have done something wrong, but I can't figure out what.

推荐答案

== 不应用于字符串比较.改用 equals().

== should not be used for String comparison. Use equals() instead.

这篇关于即使条件正确,if条件也不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆