A small case like the first season of Java (demo user login)

Source: Internet
Author: User

The first is to do a user login small case. Other logic is added on this basis. Import java.util.scanner;/* * Demo login, give three chance, and prompt several more. If the login is successful, you can play the game of guessing numbers. * * Analysis: * A: Define user name and password. Already exist. * B: Keyboard input user name and password. * C: Compare user name and password. * If all are the same, the login success * If there is a difference, then login failed * D: Give three opportunities, with loop improvement, preferably with a for loop. */public class StringTest2 {public static void main (string[] args) {//define user name and password. Already exist. String username = "admin"; String Password = "admin";//give three chance, with loop improvement, preferably with for loop. for (int x = 0; x < 3; + +) {//x=0,1,2//keyboard input username and password. Scanner sc = new Scanner (system.in); System.out.println ("Please enter user name:"); String name = Sc.nextline (); System.out.println ("Please enter password:"); String pwd = Sc.nextline ();//compare user name and password. if (name.equals (username) && pwd.equals (password)) {//If all are the same, the login succeeds System.out.println ("login successful, start playing the game");// Guess the number game guessnumbergame.start (); break;} else {//If there is a difference, the login fails//2,1,0//If this is the No. 0 time, you should change the hint if ((2-x) = = 0) {System.out.println ("The account is locked, please contact the Monitor");} else {SYSTEM.OUT.PR INTLN ("Login failed, you also have" + (2-x) + "Second Chance");}}}}


Next, improve the code. Add some logic--when the user logs in successfully, guess the number games.

Import java.util.scanner;//analog login, give three chances, and prompt several more. If the login is successful, you can play the game of guessing numbers. public class StringTest2 {public static void main (string[] args) {//define user name and password. Already exist. String username = "admin"; String Password = "admin";//give three chance, with loop improvement, preferably with for loop. for (int x = 0; x < 3; + +) {//x=0,1,2//keyboard input username and password. Scanner sc = new Scanner (system.in); System.out.println ("Please enter user name:"); String name = Sc.nextline (); System.out.println ("Please enter password:"); String pwd = Sc.nextline ();//compare user name and password. if (name.equals (username) && pwd.equals (password)) {//If all are the same, the login succeeds System.out.println ("login successful, start playing the game");// Guess the number game guessnumbergame.start (); break;} else {//If there is a difference, the login fails//2,1,0//If this is the No. 0 time, you should change the hint if ((2-x) = = 0) {System.out.println ("The account is locked, please contact the Monitor");} else {SYSTEM.OUT.PR INTLN ("Login failed, you also have" + (2-x) + "Second Chance");}}}}
Guess the code for the digital mini-game is written in a different class.
Mport java.util.scanner;/* * Then guess the code of the digital mini-game */public class Guessnumbergame {private Guessnumbergame () {}public static void St Art () {//generates a random number int numbers = (int) (Math.random () * +) + 1;while (true) {//keyboard input data Scanner sc = new Scanner (system.in); S Ystem.out.println ("Please enter the data you want to guess (1-100):"), int guessnumber = Sc.nextint ();//Determine if (Guessnumber > number) { System.out.println ("You guessed the data" + Guessnumber + "big");} else if (Guessnumber < number) {System.out.println ("you guessed the data" + Guessnumber + "small");} else {System.out.println ("Congratulations, guess") ; break;}}}

Compile and run, very fun small case.

A small case like the first season of Java (demo user login)

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.