• <i id='qQz3m'><tr id='qQz3m'><dt id='qQz3m'><q id='qQz3m'><span id='qQz3m'><b id='qQz3m'><form id='qQz3m'><ins id='qQz3m'></ins><ul id='qQz3m'></ul><sub id='qQz3m'></sub></form><legend id='qQz3m'></legend><bdo id='qQz3m'><pre id='qQz3m'><center id='qQz3m'></center></pre></bdo></b><th id='qQz3m'></th></span></q></dt></tr></i><div id='qQz3m'><tfoot id='qQz3m'></tfoot><dl id='qQz3m'><fieldset id='qQz3m'></fieldset></dl></div>

      <tfoot id='qQz3m'></tfoot>

        • <bdo id='qQz3m'></bdo><ul id='qQz3m'></ul>

        <legend id='qQz3m'><style id='qQz3m'><dir id='qQz3m'><q id='qQz3m'></q></dir></style></legend>

        <small id='qQz3m'></small><noframes id='qQz3m'>

      1. C#如何循环用户输入,直到输入的数据类型正确

        时间:2023-08-27

          • <bdo id='3AEUB'></bdo><ul id='3AEUB'></ul>
            <tfoot id='3AEUB'></tfoot>

            <small id='3AEUB'></small><noframes id='3AEUB'>

            <i id='3AEUB'><tr id='3AEUB'><dt id='3AEUB'><q id='3AEUB'><span id='3AEUB'><b id='3AEUB'><form id='3AEUB'><ins id='3AEUB'></ins><ul id='3AEUB'></ul><sub id='3AEUB'></sub></form><legend id='3AEUB'></legend><bdo id='3AEUB'><pre id='3AEUB'><center id='3AEUB'></center></pre></bdo></b><th id='3AEUB'></th></span></q></dt></tr></i><div id='3AEUB'><tfoot id='3AEUB'></tfoot><dl id='3AEUB'><fieldset id='3AEUB'></fieldset></dl></div>

                • <legend id='3AEUB'><style id='3AEUB'><dir id='3AEUB'><q id='3AEUB'></q></dir></style></legend>
                    <tbody id='3AEUB'></tbody>
                  本文介绍了C#如何循环用户输入,直到输入的数据类型正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何使这段代码循环要求用户输入直到 int.TryParse()

                  How to make this piece of code loop asking for input from the user until int.TryParse()

                  成功了吗?

                  //setX
                      public void setX()
                      {
                          //take the input from the user
                          string temp;
                          int temp2;
                          System.Console.WriteLine("Enter a value for X:");
                          temp = System.Console.ReadLine();
                          if (int.TryParse(temp, out temp2))
                              x = temp2;
                          else
                              System.Console.WriteLine("You must enter an integer type value"); 'need to make it ask user for another input if first one was of invalid type'
                      }
                  

                  有用答案后的代码版本:

                  Version of the code after the helpful answer:

                   //setX
                      public void setX()
                      {
                          //take the input from the user
                          string temp;
                          int temp2;
                          System.Console.WriteLine("Enter a value for X:");
                          temp = System.Console.ReadLine();
                          if (int.TryParse(temp, out temp2))
                              x = temp2;
                          else
                          {
                              Console.WriteLine("The value must be of integer type");
                              while (!int.TryParse(Console.ReadLine(), out temp2))
                                  Console.WriteLine("The value must be of integer type");
                              x = temp2;
                          }
                      }
                  

                  推荐答案

                  while (!int.TryParse(Console.ReadLine(), out mynum))
                      Console.WriteLine("Try again");
                  

                  public void setX() {
                      Console.Write("Enter a value for X (int): ");
                      while (!int.TryParse(Console.ReadLine(), out x))
                          Console.Write("The value must be of integer type, try again: ");
                  }
                  

                  试试这个.我个人更喜欢使用 while,但 do .. while 也是有效的解决方案.问题是我真的不想在任何输入之前打印错误消息.然而 while 也有更复杂的输入问题,不能被压入一行.这真的取决于你到底需要什么.在某些情况下,我什至建议使用 goto,即使有些人可能会因此而追踪我并用鱼打我.

                  Try this. I personally prefer to use while, but do .. while is also valid solution. The thing is that I don't really want to print error message before any input. However while has also problem with more complicated input that can't be pushed into one line. It really depends on what exactly you need. In some cases I'd even recommend to use goto even tho some people would probably track me down and slap me with a fish because of it.

                  这篇关于C#如何循环用户输入,直到输入的数据类型正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:在 Visual Studio 中而不是控制台中具有控制台应用 下一篇:在没有表格的情况下在屏幕上绘图

                  相关文章

                  最新文章

                  <small id='S85mV'></small><noframes id='S85mV'>

                • <legend id='S85mV'><style id='S85mV'><dir id='S85mV'><q id='S85mV'></q></dir></style></legend>

                    <tfoot id='S85mV'></tfoot>

                      <bdo id='S85mV'></bdo><ul id='S85mV'></ul>
                    <i id='S85mV'><tr id='S85mV'><dt id='S85mV'><q id='S85mV'><span id='S85mV'><b id='S85mV'><form id='S85mV'><ins id='S85mV'></ins><ul id='S85mV'></ul><sub id='S85mV'></sub></form><legend id='S85mV'></legend><bdo id='S85mV'><pre id='S85mV'><center id='S85mV'></center></pre></bdo></b><th id='S85mV'></th></span></q></dt></tr></i><div id='S85mV'><tfoot id='S85mV'></tfoot><dl id='S85mV'><fieldset id='S85mV'></fieldset></dl></div>