首页 > 解决方案 > Java登录系统不工作;读取第一个用户,但无法解析第二个或下一个用户

问题描述

我已经使用数组制作了一个登录系统。我制作了单独的数组来存储用户和管理员(用户名存储在用户和管理员中,密码存储在 usersPswd 和 adminsPswd 中)

static public String users[] = new String[10];
static public String usersPswd[] = new String[10];
static public String admins[] = new String[10];
static public String adminsPswd[] = new String[10];

我还创建了两个变量,每次创建用户时都会递增以将用户存储在正确的索引中,以及与登录状态相对应的状态字符串

static public int userTrackerCount = 0;
static public int adminTrackerCount = 0;
static public String status = "";

每次运行注册用户方法时,我所做的登录是将索引计数的用户设置为用户名,并将索引计数的 userspswd 设置为密码(假设用户名和密码在从扫描仪输入运行此代码之前具有值):

users[uCount] = username;
usersPassword[uCount] = password;

或注册管理员:

admins[count] = username;
adminsPassword[count] = password;

这是我的登录方法:

public void login(String username, String password, String hierarchy) {
    if ("admin".equals(status) || "user".equals(status)) {
        System.out.println("You are already logged in");
        return;
    } else {
        while ("".equals(status)) {
            if (hierarchy.equals("admin")) {
                for (int i = 0; i < admins.length; i++) {
                    if (username.equals(admins[i])) {
                        for (int a = 0; a < adminsPassword.length; a++) {
                            if (password.equals(adminsPassword[i])) {
                                System.out.println("You have successfully 
logged in!");
                                status = "admin";
                                currentUserIndex = i;
                                return;
                            } else {
                                System.out.println("Wrong password");
                                return;
                            }
                        }
                    } else {
                        System.out.println("Admin not recognized");
                        return;
                    }
                }
            } else if (hierarchy.equals("user")) {
                for (int i = 0; i < users.length; i++) {
                    if (username.equals(users[i])) {
                        for (int a = 0; a < usersPassword.length; a++) {
                            if (password.equals(usersPassword[i])) {
                                System.out.println("You have successfully 
logged in!");
                                status = "user";
                                currentUserIndex = i;
                                return;
                            } else {
                                System.out.println("Wrong password");
                                return;
                            }
                        }
                    } else {
                        System.out.println("User not recognized");
                        return;
                    }
                }
            }

        }
    }

编辑:我还有一个注销方法,将状态设置回“”,因此我在登录每个用户后运行此方法例如登录注销登录(再次)

当我在注册两个用户后运行登录方法时,第一个用户已登录但第二个用户无法解析(运行消息“用户无法识别”)。这很奇怪,因为我在注册用户后检查了 users 和 usersPswd 数组,并且用户名和密码都在那里。请帮助解决这个问题。如果您想查看更多代码,我可能无法提供(因为这是一个最终项目,并且有一个抄袭检查器)

标签: javaarrays

解决方案


您应该学习如何使用调试器,它将对您有所帮助。

问题出在这段代码中:

 1 for (int i = 0; i < users.length; i++) {
 2   if (username.equals(users[i])) {
 3     for (int a = 0; a < usersPassword.length; a++) {
 4       if (password.equals(usersPassword[i])) {
 5         System.out.println("You have successfully logged in!");
 6         status = "user";
 7         currentUserIndex = i;
 8         return;
 9       } else {
10         System.out.println("Wrong password");
11         return;
12       }
13     }
14   } else {
15     System.out.println("User not recognized");
16     return;
17   }
18 }

当第二个用户调用 login() 并且它是第 2 行的第一个循环条件时,将返回 false(第二个用户的名称与第一个用户的名称不同)。结果,第 14 行的 else 将被调用。

要更正解决方案,您可以在第 14 行删除 else 并在 for 循环后添加 if 块:

//no changes before
} else if (hierarchy.equals("user")) {
    for (int i = 0; i < users.length; i++) {
        if (username.equals(users[i])) {
            for (int a = 0; a < usersPassword.length; a++) {
                if (password.equals(usersPassword[i])) {
                    System.out.println("You have successfully logged in!");
                    status = "user";
                    currentUserIndex = i;
                    return;
                } else {
                    System.out.println("Wrong password");
                    return;
                }
            }
        }
    }
    if (!status.equals("user")) {
        System.out.println("User not recognized");
        return;
    }
}
//no changes after

推荐阅读