Java Loop Exercise: Demo Registration and Login

Source: Internet
Author: User

package practicego;import java.util.scanner;/*  *   Demo Registration and Login:     1. Registration does not know the number of user operations, so it is necessary to use the dead loop     2. After the user has determined the user name and password, the registration succeeds, save the user name and password entered by users      3. Login can only be three times, if the third time has not successfully quit the program    think  : 1. How to save a user name and password           2. Use a loop that can only perform three times to control the number of landings, but ask for a third time, "Good-bye"   */public class cto {public  static void main (String[] args)  {scanner sc = new scanner (System  .in); System.out.println ("Welcome to Registered Login Simulation system"); string username;//Save username string password;//Save Password//register while (true) {System.out.println ("Please enter user name:"); String u = sc.next (); System.out.println ("Please enter password:"); String p = sc.next (); System.out.println ("User name:" +u+ "   available"); System.out.println ("Are you sure to register with the current user name and password?") Y is     n-no "); Char rightornot = sc.next (). CharAt (0);if  (rightOrNot  ==  ' Y ')   {username = u;password = p;break;} if  (rightornot ==  ' N ')  {continue;}} SYSTEM.OUT.PRINTLN ("Congratulations! Registration Successful! Please login! ");//Login for (int i=0; i<3; i++) {System.out.println (" Please enter user name: "); String username_input = sc.next (); System.out.println ("Please enter password:"); String password_input = sc.next ();if  (Username.equals (username_input)  &&  password.equals (Password_input)) {System.out.println ("Congratulations! Login Successful! "); break;} else{if  (i==2)  {system.out.println ("Good-bye! ");} ELSE{SYSTEM.OUT.PRINTLN ("Input error, continue typing. You also have "+ (i+1) +"    chance ");}}}}

Test:

Welcome to register Login Simulation system Please enter the user name: Yeleven Please enter the password: 123456 user name: Yeleven can I use the current username and password to register? Y is n-No y congratulations! Registration Successful! Please login! Please enter your username: Yeleven Please enter your password: 123456 Congratulations! Login Successful!
Welcome to register Login Simulation system Please enter the user name: Yeleven Please enter the password: 123456 user name: Yeleven can I use the current username and password to register? Y-is n-no n Please enter your username: Yelevena Please enter your password: 654321 username: Yelevena can I use the current username and password to register? Y is n-No y congratulations! Registration Successful! Please login! Please enter your username: Yelevena Please enter your password: 654321 congratulations! Login Successful!
Welcome to register Login Simulation system Please enter the user name: Yeleven Please enter the password: 123456 user name: Yeleven can I use the current username and password to register? Y is n-No y congratulations! Registration Successful! Please login! Please enter the user name: Yeleven Please enter the password: 654321 input error, please continue to enter. You have 2 chances. Please enter your username: Yelevena Please enter your password: 123456 input error, please continue to enter. You have 1 chances. Please enter your username: Yelevena Please enter your password: 654321 goodbye!


Java Loop Exercise: Demo Registration and Login

Related Article

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.