简单的切换用户c#console应用程序

时间:2015-11-26 20:17:06

标签: c#

我是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;

    }

1 个答案:

答案 0 :(得分:0)

我不确定你为什么要使用

 //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());
}

相反,您应该使用this

之类的输入
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
        }

    }
    }
}