Class:NameII Permission:public
Method: main permission: public
Parameters: name, password, denglu, i;
Parameter introduction:
name, data type String, is used to store a value obtained from input, and is used as the storage of usernames in this program;
password, data type String, is used to store a value obtained from input, and is used as the storage of passwords in this program;
dnglu, the data type boolean, is used to store the login status of the default account. True means login is successful, and false means not logged in yet;
i, data type int, is used to store the value of the number of times a user attempted to log in;
Method Function:
Output "Please enter username:" in the console, ask the user to enter a String value from the console and store it in the name;
Then output "Please enter password" on the console, ask the user to enter a String value from the console and store it in password;
Verify that the username and password obtained from the console through input is consistent with the default username and password;
If it is consistent, the output is "Login successfully" and the user login status is changed to true;
If it is inconsistent, remind the user to log in for failure, add 1 to the number of times the user failed to log in, and remind the user to have the remaining number of attempts to log in for the user account;
The user failed to log in 3 times and prompted that the user name account was frozen.
The code copy is as follows:
public class NameII {
public static void main (String []arge) {
boolean dnglu = false;//Declare a variable of boolean data type dnglu stores the login status of the user name, the default value is false. Not logged in yet;
int i = 0;//Declare an int data type variable i to store the number of times the user attempts to log in;
java.util.Scanner input = new java.util.Scanner (System.in);
do{
System.out.println("Please enter username:");
String name = input.next();//Declare a variable name of String data type to store the value of the user name obtained in input;
System.out.println("Please enter password:");
String password = input.next();//Declare a variable password of String data type to store the value of the password obtained in input;
//Verify whether the value in name and password are consistent with the user name and password;
if ("zhang".equals(name)&&"123".equals(password)){//If the following code block is consistent, execute the following code block;
System.out.println("Login successfully");//prompt the user name to log in successfully;
denglu = true;//Change username login status;
}else{//If inconsistent, execute the following code block;
//Remind the user that login failed, add 1 to the number of times the user failed to login, and remind the user that the remaining number of attempts to login is attempted by the user;
i++;
System.out.println("Login failed, you can also try it in "+ (3-i) +" times");
//The user failed to log in 3 times and prompted that the user name account was frozen;
if(i == 3){
System.out.println("Account or password is incorrect three times, account is frozen");
}
}
}while(i < 3 && !denglu);//Satisfies users whose login failed less than three times or users whose login was successful;
}
}