猿问

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!");


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


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


千万里不及你
浏览 182回答 1
1回答

陪伴而非守候

您应该学习如何使用调试器,它会对您有所帮助。问题出在这段代码中:&nbsp;1 for (int i = 0; i < users.length; i++) {&nbsp;2&nbsp; &nbsp;if (username.equals(users[i])) {&nbsp;3&nbsp; &nbsp; &nbsp;for (int a = 0; a < usersPassword.length; a++) {&nbsp;4&nbsp; &nbsp; &nbsp; &nbsp;if (password.equals(usersPassword[i])) {&nbsp;5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("You have successfully logged in!");&nbsp;6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;status = "user";&nbsp;7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;currentUserIndex = i;&nbsp;8&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;&nbsp;9&nbsp; &nbsp; &nbsp; &nbsp;} else {10&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("Wrong password");11&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return;12&nbsp; &nbsp; &nbsp; &nbsp;}13&nbsp; &nbsp; &nbsp;}14&nbsp; &nbsp;} else {15&nbsp; &nbsp; &nbsp;System.out.println("User not recognized");16&nbsp; &nbsp; &nbsp;return;17&nbsp; &nbsp;}18 }当第二个用户调用 login() 并且它是第 2 行的第一个循环条件时,将返回 false(第二个用户的名称与第一个用户的名称不同)。因此,第 14 行的 else 将被调用。要更正解决方案,您可以在第 14 行删除 else 并在 for 循环后添加 if 块://no changes before} else if (hierarchy.equals("user")) {&nbsp; &nbsp; for (int i = 0; i < users.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (username.equals(users[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int a = 0; a < usersPassword.length; a++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (password.equals(usersPassword[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You have successfully logged in!");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status = "user";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentUserIndex = i;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Wrong password");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; if (!status.equals("user")) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("User not recognized");&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }}//no changes after
随时随地看视频慕课网APP

相关分类

Java
我要回答