Java User name password validation sample code sharing _java

Source: Internet
Author: User

Class: Nameii Permissions: Public
Method: Main Permission: Public

Parameters: Name,password,denglu,i;
Parameter introduction:
Name, a data type String, used to store a value obtained from input and used as a repository for user names in this program;
Password, a data type String, used to store a value obtained from input, which is used as a store of passwords in this program;
Denglu, a Boolean of data type, used to store the login status of the default account, True indicates successful login, False indicates not yet logged in;
I, the data type int, is used to store a user to attempt the number of logon failures;

Method Features:
In the console output "Please enter user name:" Requires the user to enter a String value from the console to store in name;
Then in the console output "Please enter a password:" To require users to enter a String from the console stored in the password;
Verify that the user name and password obtained from the console through input are consistent with the default username and password;
If consistent, output "login succeeded" and change the user login status Denglu value to true;
If inconsistent, remind the user login failed, the user attempts to login failed to add 1, and remind users of the remaining number of attempts to login;
The number of unsuccessful attempts by the user to log on has reached 3 and prompted the user name account to be frozen.

Copy Code code as follows:

public class Nameii {
public static void Main (String []arge) {
Boolean Denglu = false;//declares a variable of a Boolean data type Denglu the login state that stores the user name, and the default value is False and is not logged in;
int i = 0;//declares an int data type variable I stores the number of times the user attempted to log on;
Java.util.Scanner input = new Java.util.Scanner (system.in);

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

Verify that the value in name and the value in password are consistent with the username and password;
if ("Zhang". Equals (name) && "123". Equals (password)) {//If consistent, execute the following block of code;
SYSTEM.OUT.PRINTLN ("Login succeeded");//Prompt user name login success;
Denglu = true;//change user name login status;
}else{//if inconsistent, execute the following block of code;

Remind the user to fail the login, add 1 times to the user's attempt to log in, and remind the user of the remaining attempts to log in;
i++;
SYSTEM.OUT.PRINTLN ("Login failed, you can also try" + (3-i) + "Times");

The number of unsuccessful user attempts to log in has reached 3 times and prompts the user name account to be frozen;
if (i = = 3) {
System.out.println ("Account or password error three times, account freezes");
}

}

}while (I < 3 &&!denglu), or users who have failed to attempt to login for less than three times or who have logged on successfully;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.