有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想知道为什么我的“登录逻辑”总是假条件?

我想知道为什么当我为登录逻辑编写语法时,密码和ID总是错误的?我的代码有没有做错什么? 这是密码

public class tester {
    public static void main (String[] args){
        Data baru = new Data();

        baru.id.add("HelloGays");
        baru.password.add("hi");
        System.out.println(baru.id.toString());
        System.out.println(baru.password.toString());
        GetCredentials login = new GetCredentials(baru.id,baru.password);
    }
}

我如上所述初始化了id和密码变量,然后

public class Data {
    public ArrayList<String> id = new ArrayList<>();
    public ArrayList<String> password = new ArrayList<>();

}

这是我在class Tester中初始化的数据收集的类

public class GetCredentials {

    public GetCredentials(ArrayList id, ArrayList password){
        Scanner input = new Scanner(System.in);

        String akses,akses2;
        boolean buffer2=false,kontol=false;
        id = new ArrayList<>();


        System.out.print("Input your ID         :");
        akses = input.nextLine();
        boolean buffer = id.contains(akses);


        password = new ArrayList<>();


        System.out.print("Enter your password   :");

        akses2 = input.nextLine();
        try{
        buffer2 = (password.get(id.indexOf(akses)).equals(akses2));
        }catch(ArrayIndexOutOfBoundsException e){
            kontol = true;
        }
        if (buffer2==true){
            System.out.println("Succsessful Login");}
        else{
            kontol=true;}

        if (kontol){
            System.err.println("Wrong ID/Password");
        }

        input.close();
    }
}

以上是检查登录凭证的语法


共 (1) 个答案

  1. # 1 楼答案

    这是因为idpassword集合是空的

    id = new ArrayList<>();
    password = new ArrayList<>();
    

    这些说明将创建新的空集合,因此当您尝试获取用户密码时,它将始终失败,因为这些集合中没有单个用户或密码