A comparative study of type String user login operation exercises 27

User login operation

demand analysis

Known user name and password, please use the program to simulate user login
total to three chances, after logging, corresponding prompt

Analysis step

  1. Define the correct user name and password into the system.
  2. Define a loop executes three times.
  3. Each time the user enter a login name and password.
  4. Allowing users to enter login and password, and the system correctly compared
  5. If successful the end of the cycle, if the end of the three cycles has not been successful in trying to please is prompted after an hour.

Note:
which uses a signal flag, this method is worth a look is the flag stand

boolean flag = false;//标志3次是否登录成功!默认为是不成功的!信号标志!

Here is the complete login code:

import java.util.Scanner;

public class StringExecDemo02 {
    public static void main(String[] args) {
        //1、定义正确的用户名和密码到系统中去。
        String loginName = "zhaoliying";
        String passWord = "zhaoliying123456";

        boolean flag = false;//标志3次是否登录成功!默认为是不成功的!信号标志!
        // 2、定义一个循环执行3次。
        for (int i = 0; i < 3; i++) {
            // 3、每一次让用户输入登录名和密码。
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入登录名称:");
            String name = sc.nextLine();
            System.out.println("请输入登录密码:");
            String pass = sc.nextLine();

            //4、让用户输入的登录名和密码和系统中正确的相比较
            if (loginName.equals(name)) {
                //登录名称正确
                //判断登录密码是否正确
                if (passWord.equals(pass)) {
                    //登录密码正确
                    System.out.println("登录成功!");
                    flag = true;//改flag
                    break;//结束循环

                } else {
                    System.out.println("登录密码不正确!");
                }
            } else {
                //登录名称不正确
                System.out.println("登录名称不正确,您还剩余:" + (3-i-1) + "登录机会!");
            }
        }

        System.out.println(flag ? "欢迎进入系统" : "请一个小时后在重试!");

    }
}

Published 34 original articles · won praise 16 · views 277

Guess you like

Origin blog.csdn.net/qq_41005604/article/details/105267360