在方法最后面加上一个 return null;试试

解决方案 »

  1.   

    不是吧?这样不就成了这个method只return null?
      

  2.   

    while (flag) 循环外边必须有return语句,从编译的角度来说并不知道一定会进入循环!
      

  3.   

    给你改了一下,再编译一下看看吧,记住if else块中都要有返回值的,因为编译器不知道到底会执行到那一步,你的if里面有返回值,而else里面就没有,所以编译器报错,你照着这个思路好好找找看 private static User getPass() throws InvalidInputException
    // returns three kinds of users depending on pass
    {
    boolean flag = true;
    while (flag)
    {
    System.out.println();
    System.out.print("Please input your password: ");
    String pass = myIO.readStr(); // if pass is ENTER key, the use is an anonymous user
    if (pass.equals(""))
    return new Anonymous();
    else
    {
    list.reset();
    String p = ((University)(list.getCurrent().getElement())).getPass();
    while (list.getCurrent() != null && !pass.equals(p))
    list.advance();
    // if pass matches one of the universities' pass, then the user is
    // that university's administrator user
    if (list.getCurrent() != null)
    return new Admin();
    else
    {
    list.reset();
    HashTable table = ((University)(list.getCurrent().getElement())).getTable();
    while (list.getCurrent() != null && !table.isStudentMatched(p))
    list.advance();
    // if pass matches one of the student's pass, then the user is a
    // student user
    if (list.getCurrent() != null)
    return new StudentUser();
    else
    {
    System.out.println("Oops! Have you typed a wrong password? Try again.");
    return null;
    }
    return null;
    }
    return null;
    }

    }
    }
      

  4.   

    private static User getPass() throws InvalidInputException
            // returns three kinds of users depending on pass
            {
                    boolean flag = true;
                    User user = null;
                    while (flag)
                    {
                            System.out.println();
                            System.out.print("Please input your password: ");
                            String pass = myIO.readStr();                        // if pass is ENTER key, the use is an anonymous user
                            if (pass.equals(""))
                                    user = new Anonymous();
                            else
                            {
                                    list.reset();
                                    String p = ((University)(list.getCurrent().getElement())).getPass();
                                    while (list.getCurrent() != null && !pass.equals(p)) 
                                            list.advance();
                                    // if pass matches one of the universities' pass, then the user is 
                                    // that university's administrator user
                                    if (list.getCurrent() != null)
                                            user = new Admin();
                                    else
                                    {
                                            list.reset();
                                            HashTable table = ((University)(list.getCurrent().getElement())).getTable();
                                            while (list.getCurrent() != null && !table.isStudentMatched(p)) 
                                                    list.advance();
                                            // if pass matches one of the student's pass, then the user is a
                                            // student user
                                            if (list.getCurrent() != null)
                                                    user = StudentUser();
                                            else
                                            {
                                                    System.out.println("Oops! Have you typed a wrong password? Try again.");                                                
                                            }
                                    }
                            }
                    }
                    return user;
            }