我希望当用户输入3次错误的ID和密码(ID和PW是正确的)时它应该停止

时间:2018-11-25 11:43:38

标签: java do-while

在我的程序中,当用户输入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...");

1 个答案:

答案 0 :(得分:0)

尝试一下;

    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))));

}