繁体   English   中英

简单的切换用户c#控制台应用程序

[英]simple switch user c# console application

我是c#的初学者,我正在通过编写小型程序进行学习,这是一个用于存储和检索用户输入数据的应用程序。

我希望它可以处理多个用户

因此,我试图建立一个简单的切换用户功能,该功能可以存储以前的用户信息,同时允许其他人快速登录并进行更改。

这是我下面的代码,它首先检查以确保该程序已经运行,因此一旦它退出登录功能。

        while (pbRunning)
        {
            psPrevUser = psUserName;
            piPrevStoreWeek = piWeek;
            piPrevStoreDay = piDay;
            pbRunning = false;
        }

        psUserName = null;

然后我要求输入用户名

        //Validates user input
        while (String.IsNullOrEmpty(psUserName))
        {
            Console.Write("Please Enter the name of the account you wish to edit: ");
            psUserName = Convert.ToString(Console.ReadLine());
        }

然后,我检查是否psUserName == psPrevUser和是否调用菜单

if(psUserName == psPrevUser)
        {
            piDay = piPrevStoreDay;
            piWeek = piPrevStoreWeek;
            psPrevUser = null;

            Menu(ref piChoice, ref psUserName, ref psName, ref piMinutes, ref psSport, ref piDay, ref piWeek, ref piPrevStoreDay, ref piPrevStoreWeek, ref psPrevUser, ref piTimeArray, piAverage, ref piArraySize, ref piNum, ref piNumItems, ref pdHeight, ref pdWeight, ref pdBMI, ref piBMIWeek, ref piBMIOnce, ref pbRunning);
        }

否则它只运行正常的登录代码

我的问题是,当我在菜单中更改用户和选择更改用户时,它会覆盖psPrevUser。

例如,如果我以Dave身份登录,然后想要将用户更改为John,以快速更改Johns帐户,然后再更改回Dave,而无需再次登录,但John登录后,我无法立即覆盖psPrevUser。

我尝试了许多种方法来保存用户名,但是它总是被覆盖。

我尝试使用断点进行调试以查看发生了什么,但是我只是看不到错误发生的位置。

有人对如何正确执行此工作有任何建议吗?

编辑:这是想要所有代码的人的完整功能

        static void Login(ref int piChoice, ref string psUserName, ref string psName, ref int piMinutes, ref string psSport, ref int piDay, ref int piWeek, ref int piPrevStoreDay, ref int piPrevStoreWeek, ref string psPrevUser, ref int [] piTimeArray, int piArraySize, ref int piAverage, ref int piNum, ref int piNumItems, ref double pdHeight, ref double pdWeight, ref double pdBMI, ref int piBMIWeek, ref int piBMIOnce, ref bool pbRunning)
    {
        //This checks to see if program is running
        while (pbRunning)
        {
            psPrevUser = psUserName;
            piPrevStoreWeek = piWeek;
            piPrevStoreDay = piDay;
            pbRunning = false;
        }

        psUserName = null;

        Console.Clear();
        Console.WriteLine("                      User Login");
        Console.WriteLine("********************************************************");
        Console.WriteLine();

        //Validates user input
        while (String.IsNullOrEmpty(psUserName))
        {
            Console.Write("Please Enter the name of the account you wish to edit: ");
            psUserName = Convert.ToString(Console.ReadLine());
        }

        //if username and prevuser are the same, just load straight into the menu
        if(psUserName == psPrevUser)
        {
            piDay = piPrevStoreDay;
            piWeek = piPrevStoreWeek;
            psPrevUser = null;

            Menu(ref piChoice, ref psUserName, ref psName, ref piMinutes, ref psSport, ref piDay, ref piWeek, ref piPrevStoreDay, ref piPrevStoreWeek, ref psPrevUser, ref piTimeArray, piAverage, ref piArraySize, ref piNum, ref piNumItems, ref pdHeight, ref pdWeight, ref pdBMI, ref piBMIWeek, ref piBMIOnce, ref pbRunning);
        }

        //Lable for error message to, reload the question
        Here:

        //Validates user input
        Console.WriteLine();
        Console.Write("Please Enter week number, you wish to start from: ");
        while (!Int32.TryParse(Console.ReadLine(), out piWeek))
        {
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.Red;
            Console.Write("***ERROR*** YOU MUST ENTER A NUMBER ***ERROR***");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine();
            Console.Write("Please Enter week number, you wish to start from: ");
        }

        //Validates user input
        if (piWeek < 1 || piWeek > 52)
        {
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine();
            Console.WriteLine("***ERROR*** PLEASE ENTER NUMBER BETWEEN 1 & 52 ***ERROR***");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("Press any key to try again.");
            Console.ReadLine();
            Console.WriteLine();
            goto Here;
        }

        Console.WriteLine();

        if (File.Exists(psUserName + piWeek + ".txt"))
        {
            Console.WriteLine();

            //Validates user input
            Console.Write("Please Enter day number, you wish to start from: ");
            while (!Int32.TryParse(Console.ReadLine(), out piDay))
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("***ERROR*** YOU MUST ENTER A NUMBER ***ERROR***");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine();
                Console.Write("Please Enter day number, you wish to start from: ");
            }
        }

        //Validates user input
        if (piDay < 1 || piDay > 7)
        {
            Console.ForegroundColor = ConsoleColor.Red;

            Console.WriteLine();
            Console.WriteLine("***ERROR*** PLEASE ENTER NUMBER BETWEEN 1 & 7 ***ERROR***");
            Console.WriteLine();
            Console.ForegroundColor = ConsoleColor.White;
            Console.Write("Please Enter day number, you wish to start from: ");
            while (!Int32.TryParse(Console.ReadLine(), out piDay))
            {
                Console.WriteLine();
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("***ERROR*** YOU MUST ENTER A NUMBER ***ERROR***");
                Console.ForegroundColor = ConsoleColor.White;
                Console.WriteLine();
                Console.Write("Please Enter day number, you wish to start from: ");
            }

            Console.WriteLine();
        }

        //If a file does not exist it will create it, stating from week 1
        else if (!File.Exists(psUserName + piWeek + ".txt"))
        {
            Console.WriteLine();
            Console.WriteLine("Welcome an account is being created for you");

            //This will create a files for new user
            using (StreamWriter sw = new StreamWriter(psUserName + "1" + ".txt", true))
            {
                sw.WriteLine("Name: " + psUserName);
                sw.WriteLine("-------------------------------");
                sw.WriteLine("Time spent execising in minutes");
                sw.WriteLine("-------------------------------");
                sw.WriteLine();
                sw.WriteLine("Week - 1");
                sw.WriteLine();

                //creates a sepereate file so I can ready times into the array easier
                using (StreamWriter sw1 = new StreamWriter(psUserName + "1" + "Times.txt", true))
                {
                    sw.Write("");
                }
            }
        }

        pbRunning = true;

    }

我不确定您为什么使用

 //Validates user input
while (String.IsNullOrEmpty(psUserName))
{
            Console.Write("Please Enter the name of the account you wish to edit: ");
            psUserName = Convert.ToString(Console.ReadLine());
}

相反,你应该使用输入像这样

class Program
{
    string someUserName;

    static void Main()
    {
    while (true) // Loop indefinitely
    {
        Console.WriteLine("Enter input:"); // Prompt
        string line = Console.ReadLine(); // Get string from user
        if (line == "exit") // Check string
        {
            break;
        }
        Console.Write("You typed "); // Report output
        Console.Write(line.Length);
        Console.WriteLine(" character(s)");


        if(someUserName != line)
        {
           // New username has been detected
           someUserName = line;
           // Do extra stuff
        }

    }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM