Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何抓住字母Q_C#_String_Exception - Fatal编程技术网

C# 如何抓住字母Q

C# 如何抓住字母Q,c#,string,exception,C#,String,Exception,我是个新手。我正在尝试从用户键盘获取一个数字。当一个数字被输入时——它计数很好,当一个字母被输入时——一个未处理的异常发生。因此,我添加了try/catch,这样就可以了。我想以某种方式区分从键盘输入的字母Q,按下后结束循环。请帮忙。我设计错了:)谢谢 您可能希望在while语句中使用readline()这样构建循环 string ans = ""; while ((ans = Console.ReadLine().ToLower()) != "q") {

我是个新手。我正在尝试从用户键盘获取一个数字。当一个数字被输入时——它计数很好,当一个字母被输入时——一个未处理的异常发生。因此,我添加了try/catch,这样就可以了。我想以某种方式区分从键盘输入的字母Q,按下后结束循环。请帮忙。我设计错了:)谢谢


您可能希望在while语句中使用
readline()
这样构建循环

string ans = "";
while ((ans = Console.ReadLine().ToLower()) != "q") {
    Console.WriteLine($"your entered {ans} enter q to quit!");
}

通过使用无限的
while
循环,再加上
break
continue
语句,可以使逻辑简单得多
int.TryParse
还允许您避免捕获异常

Console.WriteLine(“程序konwertuje liczbęw systemie dziesiętnym do systemu dwójkowego\nAby opuścić程序wciśnij klawisz Q”);
while(true)
{
var ans=Console.ReadLine().ToLower();
如果(ans==“q”)
{
打破
}
Console.Clear();
Console.WriteLine(“Podaj liczbęw system ie dziesiętnym”);
如果(!int.TryParse(ans,out var编号))
{
Console.WriteLine(“Złe dane-podaj liczbęcalkowit”);
继续;
}
Console.WriteLine(“Liczba{0}w系统dwójkowym to:”,编号);
Dec_to_bin(编号);
}

我做了一个这样的外观,效果很好:

            {
                var ans = Console.ReadLine().ToLower();
                if (ans == "q")
                {
                    Console.WriteLine("koniec!");
                    break;
                }
                Console.Clear();
                Console.WriteLine("Podaj liczbę w systemie dziesiętnym");
                ans = Console.ReadLine();
                if (int.TryParse(ans, out var number))
                {

                    Console.WriteLine("Liczba {0} w systemie dwójkowym to: ", number);
                    Dec_to_bin(number);
                }
                else
                {
                    Console.WriteLine("Zła wartość. " + msg);
                }
            }```

你所拥有的看起来应该有用。你调试过它了吗?它看起来像是我想要的,但我自己也弄不明白。我要去看看,因为它看起来简单而优雅。它不像excepted那样工作,但我觉得这是一个好方法。我会把这个包裹改成服务的。祝您在未来的编码工作中好运!
            {
                var ans = Console.ReadLine().ToLower();
                if (ans == "q")
                {
                    Console.WriteLine("koniec!");
                    break;
                }
                Console.Clear();
                Console.WriteLine("Podaj liczbę w systemie dziesiętnym");
                ans = Console.ReadLine();
                if (int.TryParse(ans, out var number))
                {

                    Console.WriteLine("Liczba {0} w systemie dwójkowym to: ", number);
                    Dec_to_bin(number);
                }
                else
                {
                    Console.WriteLine("Zła wartość. " + msg);
                }
            }```