【问题标题】:C# How to loop user input until the datatype of the input is correct?C#如何循环用户输入,直到输入的数据类型正确?
【发布时间】:2011-06-27 04:14:12
【问题描述】:

如何让这段代码循环要求用户输入直到 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'
    }

有用答案后的代码版本:

 //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;
        }
    }

【问题讨论】:

    标签: c# loops input console


    【解决方案1】:
    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,即使有些人可能会因此而追踪我并用鱼打我。

    【讨论】:

    • 谢谢。我用更新的代码编辑了我的原始帖子。有没有办法让这段代码更短,但它会做同样的事情?只是好奇。
    • 我已经为你编辑了一个较短的整个 setX 方法的解决方案
    【解决方案2】:

    即使问题已被标记为已回答,do-while 循环也更适合验证用户输入。

    注意你的代码:

    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");
    

    顶部和底部的代码相同。这可以更改:

    do {
        Console.WriteLine("The value must be of integer type");
    } while (!int.TryParse(Console.ReadLine(), out temp2));
    

    【讨论】:

    • 谢谢。这就是我一直在寻找的。​​span>
    【解决方案3】:

    我一直想知道很多,但我只是想通了!

        int number;
        bool check;
        do
        {
            Console.WriteLine("Enter an integer:");
            check = int.TryParse(Console.ReadLine(), out num1);
        }
        while (!check);
    

    此代码将循环,直到用户输入一个整数。这样,程序不会简单地报告错误,而是立即允许用户再次输入另一个正确的值。

    【讨论】:

      【解决方案4】:

      这也有帮助

      public int fun()
      {
          int Choice=0;
          try 
          {
              Choice = int.Parse(Console.ReadLine());
              return choice;
          } 
          catch (Exception) 
          {
              return fun(); 
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-11-19
        • 2021-07-16
        • 2020-08-15
        • 2014-12-17
        • 2017-02-15
        • 1970-01-01
        • 2021-09-09
        • 1970-01-01
        相关资源
        最近更新 更多