C# While TryParse

后端 未结 4 1364
长情又很酷
长情又很酷 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:10

    You're really close, I think something like this will work well for you:

    char typeClient = ' ';
    while (typeClient != 'c' && typeClient != 'g')
    {
        Console.WriteLine("\nEntrez le type d'employé (c ou g) : ");
        var line = Console.ReadLine();
        if (!string.IsNullOrEmpty(line)) { typeClient = line[0]; }
        else { typeClient = ' '; }
    }
    

    basically it reads the input into the typeClient variable when the user enters something so the loop will continue until they enter g or c.

    0 讨论(0)
  • 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-23 09:14

    You can use ConsoleKeyInfo with Console.ReadKey() :

     ConsoleKeyInfo keyInfo;
     do {
       Console.WriteLine("\nEntrez le type d'employé (c ou g) : ");
       keyInfo = Console.ReadKey();
     } while (keyInfo.Key != ConsoleKey.C && keyInfo.Key != ConsoleKey.G);
       
    
    0 讨论(0)
  • 2021-01-23 09:32
    string input;
    do {
        Console.WriteLine("Entrez le type d'employé (c ou g):");
        input = Console.ReadLine();
    } while (input != "c" && input != "g");
    
    char typeClient = input[0];
    
    0 讨论(0)
提交回复
热议问题