《零基础学C#》第六章-实例02:模拟用户登录——练习2

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wtxhai/article/details/88634928
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = null; //创建用户账号变量,初始值为null
            string password = null; //创建用户密码变量,初始值为null
            string email = null;//创建用户邮箱变量,初始值为null
            string phone = null;//创建用户电话变量,初始值为null
            string address = null;//创建用户住址变量,初始值为null
            Console.WriteLine("欢迎来到XXX网,请新用户注册账号!\n");

            bool registerNotSuccess = true;//判断用户注册是否成功
            do
            {
                bool nameFlag = true;//验证用户名
                do
                {
                    Console.Write("请输入账户名:");
                    userName = Console.ReadLine();//记录用户名
                    if ("".Equals(userName))//判断用户名是否为空
                    {
                        Console.WriteLine("用户名不能为空,请重新输入!");
                    }
                    else
                    {
                        nameFlag = false;
                    }
                } while (nameFlag);

                bool pwdFlag = true;//验证密码
                do
                {
                    Console.Write("请输入密码:");
                    String pwdTmp1 = Console.ReadLine();//记录首次输入密码
                    Console.Write("请再次输入密码:");
                    String pwdTmp2 = Console.ReadLine();//记录第二次输入密码
                    if (pwdTmp1.Equals(pwdTmp2))//判断密码是否一致
                    {
                        pwdFlag = false;
                        password = pwdTmp1;
                    }
                    else
                    {
                        Console.WriteLine("对不起,您两次输入的密码不一致,请重新输入!");
                    }
                } while (pwdFlag);

                Console.Write("请输入邮箱地址:");
                email = Console.ReadLine();//记录Email

                bool phoneFlag = true;//验证电话号码
                do
                {
                    Console.Write("请输入电话:");
                    phone = Console.ReadLine();//记录电话号码
                    if (phone.Length != 11)//判断长度是否为11
                    {
                        Console.WriteLine("输入的电话号码有误,请重新输入!");
                    }
                    else
                    {
                        phoneFlag = false;
                    }
                } while (phoneFlag);

                Console.Write("请输入住址(可选):");
                address = Console.ReadLine();//记录地址

                bool notSure = true;//核对用户注册信息
                do
                {
                    Console.WriteLine("\n****请您核对注册的信息****");
                    Console.WriteLine("账号:" + userName);
                    Console.WriteLine("邮箱:" + email);
                    Console.WriteLine("电话:" + phone.Substring(0, 3) + "****" + phone.Substring(7));//电话号中间4位用*代替
                    Console.WriteLine("住址:" + address);
                    Console.WriteLine("************************");
                    Console.Write("确认请输入Y,重新注册请输入N:");
                    String answer = Console.ReadLine();//确认是否信息是否正确
                    if (answer.ToUpper().Equals("Y"))//如果输入Y,则表示正确
                    {
                        registerNotSuccess = false;
                        notSure = false;
                    }
                    else if (answer.ToUpper().Equals("N"))//如果输入N,表示错误
                    {
                        registerNotSuccess = true;
                        notSure = false;
                    }
                    else
                    {
                        Console.WriteLine("您的输入有误,请重新输入");
                    }
                } while (notSure);
            } while (registerNotSuccess);

            Console.WriteLine("\n欢迎来到XXX网,请登录!");

            bool loginFlag = true;//判断登录是否成功
            do
            {

                Console.Write("请输入账号:");
                String inputName = Console.ReadLine();//记录用户名
                Console.Write("请输入密码:");
                String inputPwd = Console.ReadLine();//记录密码
                if (null != userName && null != password)//判断用户名和密码是否为空
                {
                    if (userName == inputName && password == inputPwd)//判断用户名和密码是否正确
                    {
                        Console.WriteLine("欢迎登录," + userName);//显示登录用户信息
                        loginFlag = false;
                    }
                    else
                    {
                        Console.WriteLine("您输入的账号密码有误,请重新输入!");
                    }
                }
            } while (loginFlag);
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/wtxhai/article/details/88634928
今日推荐