猿问

关于字符串检查的 if 语句无法访问代码

由于某种原因,我收到一条错误消息,指出该线路currentUser = gar.getAccount(userN);无法访问,但实际上不应该如此。Garage.getAccount()只是从 a 检索Hashmap。两个 if 语句并不相互矛盾,我尝试添加 else 语句,但无论它说什么,该行都是无法访问的。


package main;


import java.util.Scanner;


public class Main {


    private static Scanner input;

    private static Garage gar;

    private static Attendant currentUser;

    private static boolean isManager;


    public static void main(String[] args) {

        input = new Scanner(System.in);

        gar = new Garage(10, 80, 10);

        currentUser = null;

        while (currentUser == null)

            logIn();

    }


    public static void logIn() {

        System.out.println("Enter username: ");

        String userN = input.nextLine();

        System.out.println("Enter password:");

        String userP = input.nextLine();

        //if no username, go back

        if(gar.getAccount(userN) == null) { 

            error("Incorrect username");

            return;

        } 

        if(gar.getAccount(userN).getPassword().equals(userP) == false); { //if entered password doesn't match

            error("Incorrect password");

            return;

        } 

        currentUser = gar.getAccount(userN);

        return;

    }


    //update to throw error pop-up later

    public static void error(String er) { System.out.println(er); }

}


MYYA
浏览 77回答 1
1回答

qq_花开花谢_0

您的语法不正确,if 语句的条件不应以分号结束。 package main;    import java.util.Scanner;    public class Main {        private static Scanner input;        private static Garage gar;        private static Attendant currentUser;        private static boolean isManager;        public static void main(String[] args) {            input = new Scanner(System.in);            gar = new Garage(10, 80, 10);            currentUser = null;            while (currentUser == null)                logIn();        }        public static void logIn() {            System.out.println("Enter username: ");            String userN = input.nextLine();            System.out.println("Enter password:");            String userP = input.nextLine();            //if no username, go back            if(gar.getAccount(userN) == null) {                 error("Incorrect username");                return;            }             if(gar.getAccount(userN).getPassword().equals(userP) == false) { //if entered password doesn't match                error("Incorrect password");                return;            }             currentUser = gar.getAccount(userN);            return;        }        //update to throw error pop-up later        public static void error(String er) { System.out.println(er); }    }
随时随地看视频慕课网APP

相关分类

Java
我要回答