簡體   English   中英

我希望當用戶輸入3次錯誤的ID和密碼(ID和PW是正確的)時它應該停止

[英]I want that it should stop when the user entered 3 times wrong ID and Password (ID and PW are correct ones)

在我的程序中,當用戶輸入了3次錯誤的ID和密碼(ID和PW是正確的)時,它就結束了-結束沒有問題-同時在說出“您的帳戶有被阻止”。

    Scanner enter = new Scanner(System.in);

    String ID2, PW2, PW = "qwerty123", ID = "theman" ; 
    int entry=0;
    do {entry++;
    System.out.println("Please enter you ID.");
        ID2 = enter.nextLine();
    System.out.println("Please enter your password.");  
        PW2 = enter.nextLine();

    if(!(ID.equals(ID2)) && ((PW.equals(PW2)))) {
        System.out.println("Try again!");
    }
    else if((ID.equals(ID2)) && (!(PW.equals(PW2)))) {
        System.out.println("Try again!");
    }
    else if(!(ID.equals(ID2)) && !((PW.equals(PW2)))) {
        System.out.println("Try again!");
    }   
    if(entry==3) {
        System.out.println("**Your account has been blocked**");
        break;
    }                       
    }while(!(ID.equals(ID2)) && !((PW.equals(PW2))));
    System.out.println("Welcome...");

嘗試這個;

    Scanner enter = new Scanner(System.in);

    String ID2, PW2, PW = "qwerty123", ID = "theman";
    int entry = 0;
    do {
        entry++;
        System.out.println("Please enter you ID.");
        ID2 = enter.nextLine();
        System.out.println("Please enter your password.");
        PW2 = enter.nextLine();

        if (ID.equals(ID2) && (PW.equals(PW2))) {
            System.out.println("Welcome...");
        } else if (!(ID.equals(ID2)) && ((PW.equals(PW2)))) {
            System.out.println("Try again!");
        } else if ((ID.equals(ID2)) && (!(PW.equals(PW2)))) {
            System.out.println("Try again!");
        } else if (!(ID.equals(ID2)) && !((PW.equals(PW2)))) {
            System.out.println("Try again!");
        }
        if (entry == 3) {
            System.out.println("**Your account has been blocked**");
            break;
        }
    } while (!(ID.equals(ID2)) && !((PW.equals(PW2))));

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM