亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

關于字符串檢查的 if 語句無法訪問代碼

關于字符串檢查的 if 語句無法訪問代碼

MYYA 2023-09-06 16:03:13
由于某種原因,我收到一條錯誤消息,指出該線路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); }}
查看完整描述

1 回答

?
qq_花開花謝_0

TA貢獻1835條經驗 獲得超7個贊

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

    }


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 107 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號