1 回答

TA貢獻1757條經驗 獲得超8個贊
您應該學習如何使用調試器,它會對您有所幫助。
問題出在這段代碼中:
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
添加回答
舉報