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