本文介绍了在TryParse中使用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

char typeClient = ' ';
bool clientValide = false;
while (!clientValide)
{
     Console.WriteLine("\nEntrez le type d'employé (c ou g) : ");
     clientValide = char.TryParse(Console.ReadLine(), out typeClient);
     if (clientValide)
         typeClient = 'c';
}

我想这样做,除非char是'g'或'c',否则它不会退出一段时间帮助 ! :)

I'd like to make it so it doesn't exit the while unless the char is 'g' or 'c'help ! :)

推荐答案

是否使用Console.ReadLine,用户必须在按或 > g .使用ReadKey代替,以便响应是瞬时的:

Is you use Console.ReadLine, the user has to press after pressing or . 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;
    }
}

这篇关于在TryParse中使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 15:37
查看更多