我正在为学校作业创建一个简单的程序,该程序创建一个非常基本的用户帐户,并将用户输入的密码与帐户密码进行比较。无论我将什么值传递给checkPassword函数,它总是返回false值。
到目前为止,我已经按照您在此处看到的功能编写了该函数,并将其分解为if else语句并根据该值返回了一个值。
我已经从main方法和checkPassword方法中显示了要与屏幕进行比较的值,以便我知道这些值是匹配的。
package firstAssignment;
public class UserAccount {
// Fields
private String username;
private String password;
private boolean active;
// Constructors
public UserAccount() {
this.active = true;
}
public UserAccount(String username, String password) {
this.username = username;
this.password = password;
this.active = true;
}
// Getters and setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean isActive() {
return active;
}
public void setActive(boolean active) {
this.active = active;
}
// Methods
public boolean checkPassword(String password) {
return (this.password == password);
}
public void deactivateAccount() {
this.active = false;
}
@Override
public String toString() {
return this.username;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserAccount other = (UserAccount) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}
}
无论我提供什么值,它总是返回false值。