C#简单登录注册系统练习

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;//控制文件流
using System.Text;

namespace 简单登录注册系统__20180808
{
class Program
{
static void Main(string[] args)
{

        Inter();
        Console.ReadKey();
    }
    //界面
    public static void Inter()
    {
        Console.WriteLine("*********************************");
        Console.WriteLine("*        欢迎来到本系统         *");
        Console.WriteLine("*          请选择操作           *");
        Console.WriteLine("*          1.登录账户           *");
        Console.WriteLine("*          2.注册账户           *");
        Console.WriteLine("*          3.修改密码           *");
        Console.WriteLine("*          4.找回密码           *");
        Console.WriteLine("*          5.退出               *");
        Console.WriteLine("*********************************");
        int n = int.Parse(Console.ReadLine());
        switch (n)
        {
            case 1: Register(); break;
            case 2: RegisteredAaccount(); break;
            case 3: Console.WriteLine(); break;
            case 4: ForgotPassword(); break;
            case 5: Quit(); break;
            default:
                Console.WriteLine("输入错误");
                break;
        }
    }

    //登录失败
    public static void LogonFailed()
    {
        Console.WriteLine("*********************************");
        Console.WriteLine("*          请选择操作           *");
        Console.WriteLine("*          1.重新登录           *");
        Console.WriteLine("*          2.注册账户           *");
        Console.WriteLine("*          3.找回密码           *");
        Console.WriteLine("*          4.退出               *");
        Console.WriteLine("*********************************");
        for (int i = 0; i < 3; i++)
        {
            int n = int.Parse(Console.ReadLine());
            switch (n)
            {
                case 1: Register(); break;
                case 2: RegisteredAaccount(); break;
                case 3: ForgotPassword(); break;
                case 4: Quit(); break;
                default:
                    Console.WriteLine("输入错误,请重新输入");
                    break;
            }
            if (2-i==0)
            {
                Quit();
            }
            Console.WriteLine("您还有{0}次机会",2-i);
        }         

    }

    //登录
    public static void Register()
    {
        string[] str = File.ReadAllLines(@"C:\Users\a\Desktop\ZSQ\message.txt");      
        Console.WriteLine("请出入账号");
        string account = Console.ReadLine();
        Console.WriteLine("请输入密码");
        string password = Console.ReadLine();

        for (int i = 1; i < str.Length; i+=2)
        {
            if (account==str[i-1])
            {
                if (password==str[i])
                {
                    Console.WriteLine("登录成功");
                    Inter();
                }
            }
            if (i>=str.Length-2)
            {
                Console.WriteLine("登录失败");
                LogonFailed();

            }

        }

    }


    //注册账户
    public static void RegisteredAaccount()
    {
        string[] str = File.ReadAllLines(@"C:\Users\a\Desktop\ZSQ\message.txt");
        for (int n = 0; true ; n++)
        {
            bool bo = false;
            Console.WriteLine("请输入账号");
            string account = Console.ReadLine();
            if (str.Length == 0)
            {
                File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt", account);//在原有文本文件上添加
                bo = true;
            }
            else
            {
                for (int i = 0; i < str.Length; i += 2)
                {
                    if ( account== str[i])
                    {
                        Console.WriteLine("该账号已经被注册,请重新输入");
                        break;
                    }
                    if (i == str.Length - 2)
                    {
                        File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt","\n"+ account);//在原有文本文件上添加
                        bo = true;
                        break;
                    }
                }

            }

            if (bo==true)
            {
                break;
            }
        }


        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("请输入密码");
            string password = Console.ReadLine();
            Console.WriteLine("请再确认密码");
            string password1 = Console.ReadLine();
            if (password==password1)
            {
                Console.WriteLine("请输入验证码");
                int authCodes = authCode();
                Console.WriteLine(authCodes);
                int auth = int.Parse(Console.ReadLine());
                if (auth==authCodes)
                {
                    File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt", "\n" + password);//在原有文本文件上添加

                    Console.WriteLine("注册成功!");
                    Inter();
                }
                else
                {
                    Console.WriteLine("注册失败");
                    RegisteredAaccount();
                }

            }
            else
            {
                if (2-i==0)
                {
                    Inter();
                }
                Console.WriteLine("请重新输入密码,还有{0}次机会",2-i);
            }

        }



    }

    //修改密码
    public static void changePassword()
    {
        Console.WriteLine("请输入账号");
        string count = Console.ReadLine();
        for (int i = 0; i < 3; i++)
        {
            Console.WriteLine("请输入新密码");
            string password1 = Console.ReadLine();
            Console.WriteLine("请再次输入密码");
            string password2 = Console.ReadLine();
            if (password1 == password2)
            {
                Inter();
            }
        }

    }


    //找回密码
    public static void ForgotPassword()
    {
        string[] str = File.ReadAllLines(@"C:\Users\a\Desktop\ZSQ\message.txt");
        Console.WriteLine("请输入账号");
        string account = Console.ReadLine();
        for (int i = 0; i < str.Length; i += 2)
        {
            if (str[i] == account)
            {
                for (int j = 0; j < 3; j++)
                {
                    Console.WriteLine("请输入密码包含字符串");
                    string passWorldStr = Console.ReadLine();
                    if (str[i+1].Contains(passWorldStr))
                    {
                        for (int k = 0; k < 3; k++)
                        {
                            Console.WriteLine("请输入新密码");
                            string password = Console.ReadLine();
                            Console.WriteLine("请再次确认新密码");
                            string password1 = Console.ReadLine();
                            if (password==password1)
                            {
                                Console.WriteLine("请输入验证码");
                                int authCodes = authCode();
                                Console.WriteLine(authCodes);
                                int auth = int.Parse(Console.ReadLine());
                                if (auth == authCodes)
                                {
                                    str[i + 1] = password;
                                    for (int m = 0; m < str.Length; m++)
                                    {
                                        if (m==0)
                                        {

                                            File.WriteAllText(@"C:\Users\a\Desktop\ZSQ\message.txt",str[m]);//在原有文本文件替换
                                        }
                                        else
                                        {
                                            File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt", "\n" + str[m]);//在原有文本文件上替换
                                        }
                                    }
                                    //File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt", "\n" + str[i]);//在原有文本文件上添加
                                    //File.AppendAllText(@"C:\Users\a\Desktop\ZSQ\message.txt", "\n" + password);//在原有文本文件上添加
                                    Console.WriteLine("找回成功!");
                                    Inter();
                                }
                                else
                                {
                                    Console.WriteLine("找回密码失败");
                                    RegisteredAaccount();
                                }
                            }
                        }                          
                    }
                    else
                    {
                        if (2-j==0)
                        {
                            Quit();
                        }
                        Console.WriteLine("输入错误,还有{0}次机会",2-j);

                    }
                }

            }               
        }
    }


    //退出
    public static void Quit()
    {
        Console.WriteLine("退出成功");
        Console.ReadKey();
    }

    //验证码
    public static int  authCode()
    {
        Random rand1 = new Random();
        int a = rand1.Next(1000,9000);
        return a;
    }


}

}

猜你喜欢

转载自blog.csdn.net/qq_42016542/article/details/81530556
今日推荐