我是Java的新手,正在尝试使用列表进行登录,但是验证方法无效。
我尝试使用计数器访问列表的值,并尝试将其分配给变量,在调试模式下,我获取的值是正确的,但条件仍然不起作用(用户:test,password测试)
这是我的用户类,其中包含设置和获取密码的方法。
public class user {
String user;
String pass;
public user(String u, String p) {
user = u;
pass = p;
}
public String getUsername() {
return user;
}
public String getPassword() {
return pass;
}
public void setUsername(String username) {
this.user = username;
}
public void setPassword(String password) {
this.user = password;
}
}
这是登录课程
import java.util.ArrayList;
import java.util.Scanner;
public class LogIn {
public void Welcome() {
System.out.println("Welcome, please enter your username");
Scanner input = new Scanner(System.in);
String username = input.next();
System.out.println("Enter your password");
String password = input.next();
this.validate(username);
}
private void validate(String username){
int attemps = 0;
ArrayList<user> users = new ArrayList<user>();
user User = new user("test", "test");
//Here I add the new user with the fixed value of test for password and user
users.add(User);
for(int i=0; i<=users.size()-1;i++){
while (attemps<=3){
System.out.println(users.get(i).getUsername());
String test = users.get(i).getUsername();
//This condition here is the one not working and with the user already added to the list and getting the correct value in the variable it still does not work
if(username == test){
System.out.println("Here");
attemps = 3;
}else{
System.out.println("invalid credentials");
attemps++;
}}
System.out.println("You have exceed the number of attempts");
}
}
}
使用用户的凭据测试并测试不起作用的密码