【问题标题】:Method is returning false but conditions match and should be returning true方法返回 false 但条件匹配并且应该返回 true
【发布时间】: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


【解决方案1】:

您需要将 return false 放在循环之外,以在返回 false 之前检查每个用户

for (User s : users) {
//if just one of my objects is librarian it will return true.
    if (s.getUserType() == User.enumUserType.LIBRARIAN) {
        return true;
     }    
}
return false;

或者使用anyMatch

return users.stream().anyMatch(s -> s.getUserType() == User.enumUserType.LIBRARIAN);

如果您的意图是在未找到用户的情况下真正抛出异常(目前在您的代码中无法访问,您会抛出该异常而不是返回

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;
        }    
    }
    throw new IllegalArgumentException("Username or password is incorrect");
}

或在流中

users.stream()
      .filter(s -> s.getUserType() == User.enumUserType.LIBRARIAN)
      .findAny()
      .orElseThrow(() -> new IllegalArgumentException("Username or password is incorrect"));

【讨论】:

  • 嘿,伙计,谢谢你的评论,不过我仍然遇到同样的问题,如果他们都不是图书馆员,这是错误的,但如果他们中的任何一个是图书馆员,则为真的。我尝试了您发布的两种解决方案。
  • 是的,如果上面有任何是利比里亚人,则返回 true。你想检查是否所有人都是利比里亚人吗? @awyeanah2
  • 我最终修复了它,我使用 verifylibrarian(username) 中的参数过滤了结果,因此如果用户名等于数组列表中的用户名并且它们是图书管理员,则返回 true。感谢您帮助我朝着正确的方向前进。
猜你喜欢
  • 1970-01-01
  • 2013-09-20
  • 2014-10-21
  • 2020-02-12
  • 1970-01-01
  • 2016-04-16
  • 1970-01-01
  • 2012-07-07
  • 2016-12-05
相关资源
最近更新 更多