Java username and password authentication sample code sharing

  • 2020-04-01 03:12:11
  • OfStack

Class: NameII      Authority: public
Methods: main      Authority: public

Parameters: the name, password, denglu, I;
Parameter description:
Name, data type String, used to store a value from the input, in this program with the role of the name of the deposit;
Password, data type String, is used to store a value obtained from the input, which is used as the password storage in this program;
Denglu, data type Boolean, is used to store the login status of the default account.
I, data type int, used to store the value of the number of times a user tried to log in failed;

Method functions:
In the console output "please enter user name:" requires the user to input a String type value from the console to be stored in the name;
Then output "please enter password:" in the console to ask the user to enter a String type of value stored in password from the console;
Verify that the user name and password obtained from the console through input are consistent with the default user name and password;
If it is consistent, output "login successfully" and change the value of denglu of user login status to true;
If it is inconsistent, remind the user of login failure, add 1 to the number of failed attempts by the user, and remind the user of the remaining number of attempts to log in;
The number of users trying to log in fails to reach 3 times, and the user name account is prompted to be frozen.


public class NameII {
 public static void main (String []arge) {
  boolean denglu = false;//Declare a Boolean data type variable denglu to store the login status of the user name, with the default value of false not yet logged in.
  int i = 0;//Declare an int data type variable I to store the number of failed attempts to log in;
  java.util.Scanner input = new java.util.Scanner (System.in);

  do{
   System.out.println(" Please enter user name: ");
   String name = input.next();//Declare a String data type variable name to store the value of the user name obtained in the input;
   System.out.println(" Please enter your password: ");
   String password = input.next();//Declare a String data type variable password to store the value of the password obtained in the input;

   //Verify that the values in name and password are the same as the user name and password.
   if ("zhang".equals(name)&&"123".equals(password)){//If consistent, execute the following code block;
    System.out.println(" Login successful ");//Prompt the user name to login successfully;
    denglu = true;//Change the login status of the user name;
   }else{//If not, execute the following code block;

   //Remind the user of login failure, add 1 to the number of failed attempts by the user, and remind the remaining number of login attempts by the user account;
    i++;
    System.out.println(" Login failed, you can still try " + (3-i) +" time ");

    //The number of users trying to log in fails to reach 3 times, and the user name account is prompted to be frozen;
    if(i == 3){
     System.out.println(" Account number or password error three times, account freeze "); 
    }

   }

  }while(i < 3 && !denglu);//Meet       Failed attempts to log in less than three times     Or       Users who log in successfully;
 }
}


Related articles: