【问题标题】:Unreachable code from if-statement about String checks关于字符串检查的 if 语句中无法访问的代码
【发布时间】:2020-01-28 20:04:42
【问题描述】:

由于某种原因,我收到一条错误消息,指出currentUser = gar.getAccount(userN); 行无法访问,但它不应该如此。 Garage.getAccount() 只是对 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); }
}

【问题讨论】:

  • if(gar.getAccount(userN).getPassword().equals(userP) == false); 分号终止if 正文。去掉它。投票结束是错字。

标签: java if-statement unreachable-code


【解决方案1】:

您的语法不正确,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); }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-01
    • 2015-12-02
    • 1970-01-01
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多