如果用户输入退格键,如何删除写在控制台中的字符?

时间:2018-12-16 15:26:22

标签: c# while-loop console.writeline

using System;

namespace login.system
{
    class code
    {
        static void system(string[] args)
        {
            Console.Write("Enter a username:");
            string username = Console.ReadLine();

            Console.Write("Enter your first name:");
            string name = Console.ReadLine();

            Console.Write("password: ");
            string password = null;
            bool flag = true;

            while (flag)
            {
                var key = Console.ReadKey(true);

                while (key.Key != ConsoleKey.Enter)
                {
                     while (key.Key != ConsoleKey.Backspace)
                     {
                          password += key.KeyChar;
                          Console.Write("*");
                     }

                     // This bit here is meant to write a backspace character 
                     // from the console, removing an * if backspace is 
                     // entered... but it's not.
                     if (key.Key == ConsoleKey.Backspace)
                     {
                          Console.Write("\b");
                     }

                     if (key.Key == ConsoleKey.Enter)
                     {
                         flag = false;
                     }
                }
           }
     }
}

当用户输入并回车以继续时,还会打印一个*,我该如何解决

0 个答案:

没有答案