【发布时间】:2019-08-30 08:39:04
【问题描述】:
我目前有一些用户对象的类型为 enum enumUserType,但其中一个对象的 enumUserType 为 LIBRARIAN。图书馆员用户需要有特殊权限,在这种情况下,它会有一个不同的菜单可以访问。
我尝试循环遍历用户数组列表,如果该用户的用户类型为图书管理员,则返回 true,如果是其他内容,则返回 false。
经过一些测试,似乎即使我的对象中只有一个是图书管理员,那么整个方法也会返回 true。然后我不能将不同的用户类型引导到不同的菜单路径。 我的第一个对象不是图书管理员,但第二个对象是。
public boolean verifyLibrarian() {
for (User s : users) {
//if just one of my objects is librarian it will return true.
if (s.getUserType() == User.enumUserType.LIBRARIAN) {
return true;
}
else
{
return false;
}
}
throw new IllegalArgumentException("Username or password is
incorrect");
}
这也是我的 while 循环:
while(exit == 0)
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter your user name");
String userName = scanner.nextLine();
System.out.println("Enter your password name");
String passWord = scanner.nextLine();
if (library.verifyLogin(userName, passWord)== true && library.verifyLibrarian() != true)
{
this.currentLoginUser = userName;
mainMenuAfterLogin();
}
//because my method is returning true, even logged in non librarians
//will get lead down to this menu
else if(library.verifyLogin(userName, passWord) == true &&
library.verifyLibrarian() == true)
{
this.currentLoginUser = userName;
librarianMenuEditBook();
}
}
如果您需要更多信息,请告诉我。 非常感谢您的帮助。
【问题讨论】:
-
for (User s : users) {这个循环只会被迭代一次 -
在您的第一次迭代中:它不是图书管理员。
标签: java foreach while-loop enums