C# While TryParse

后端 未结 4 1371
长情又很酷
长情又很酷 2021-01-23 08:45
char typeClient = \' \';
bool clientValide = false;
while (!clientValide)
{
     Console.WriteLine(\"\\nEntrez le type d\'employé (c ou g) : \");
     clientValide = cha         


        
4条回答
  •  执念已碎
    2021-01-23 09:12

    Is you use Console.ReadLine, the user has to press Enter after pressing c or g. Use ReadKey instead so that the response is instantaneous:

    bool valid = false;
    while (!valid)
    {
        Console.WriteLine("\nEntrez le type d'employé (c ou g) : ");
        var key = Console.ReadKey();
        switch (char.ToLower(key.KeyChar))
        {
            case 'c':
                // your processing
                valid = true;
                break;
            case 'g':
                // your processing
                valid = true;
                break;
            default:
                Console.WriteLine("Invalid. Please try again.");
                break;
        }
    }
    

提交回复
热议问题