验证几个数字用户输入 C# 控制台

validate several numeric user inputs C# Console

好的,所以我为一个小型学校项目制作了这个程序。它只是一个小型控制台应用程序,用户应该输入 10 个数字并获得平均结果、最高和最低数字。 但是如果 he/she 输入的不是数字,我想更正用户。这是我的代码:

class Program
{
    static void Main(string[] args)
    {
        int num1, num2, num3, num4, num5, num6, num7, num8, num9, num10;

            Console.Write("Enter the 1st number: ");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 2nd number: ");
            num2 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 3rd number: ");
            num3 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 4th number: ");
            num4 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 5th number: ");
            num5 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 6th number: ");
            num6 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 7th number: ");
            num7 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 8th number: ");
            num8 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 9th number: ");
            num9 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the 10th number: ");
            num10 = Convert.ToInt32(Console.ReadLine());

            int[] numbers = { num1, num2, num3, num4, num5, num6, num7, num8, num9, num10 };
            int biggestNumber = numbers.Max();
            int smallestNumber = numbers.Min();
            Console.WriteLine("Your biggest number was: " + biggestNumber);
            Console.WriteLine("Your smallest number was: " + smallestNumber);

            int result = (num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10) / 10;
            Console.WriteLine("The average of your numbers are: " + result);

            Console.ReadLine();
        }
    }
}

在网上研究后,我发现这是一个可能的解决方案:

            Console.Write("Enter the 1st number: ");
            while (Int32.TryParse(Console.ReadLine(), out num1) == false)
               {
                Console.WriteLine("Type a number please.");
               }

虽然这可行,但要对代码中的每个条目(num2、num3、num4 等)执行此操作。所以我想也许这是一种更简单的方法,而不是为每个用户输入复制粘贴代码块。

我的问题是:是否有更简单/更简化的解决方案来验证所有用户输入(也许一次)?

编辑: 如果你能帮助我,我很想看看你将如何在我的代码中实现你的解决方案:)

这只是一个想法,基本上你循环直到用户输入正确的数字十次。输入正确后,数字保存在input[indexOfInput]

static void Main(string[] args)
{
    int[] input = new int[10];
    for (int i = 0; i < 10; i++)
    {
        Console.WriteLine("Write " + (i + 1) + " number");
        int temp;
        while (!int.TryParse(Console.ReadLine(), out temp)
        {
            Console.WriteLine("Write " + (i + 1) + " number");
        }
        input[i] = temp;
    }
    // at this point you have all 10 numbers written by the user
}

伙计,你需要这样的东西。收集所有值到 List 并使用它:

        List<int> numbers = new List<int>();
        while (numbers.Count() < 10)
        {
            Console.Write("Enter the " + (numbers.Count() + 1) + "st number: ");
            int num;
            while (Int32.TryParse(Console.ReadLine(), out num) == false)
            {
                Console.WriteLine("Type a number please.");
            }
            numbers.Add(num);
        }

        int biggestNumber = numbers.Max();
        int smallestNumber = numbers.Min();
        Console.WriteLine("Your biggest number was: " + biggestNumber);
        Console.WriteLine("Your smallest number was: " + smallestNumber);

        int result = numbers.Sum()/numbers.Count();
        Console.WriteLine("The average of your numbers are: " + result);

        Console.ReadLine();

我会使用 foreach 循环,但 while 循环也可以。

这是我的解决方案

  var myList = new List<string>() { "1", "number2", "3", "4", "5", "6", "7", "8" };
  var numbers = new List<int>();

  foreach (var str in myList)
  {
    int myInt;
    if (!int.TryParse(str, out myInt))
      Console.WriteLine(string.Format("{0} is not a number. Type a number please.", str));
    else
      // Otherwise add the number to a new list of int's
      numbers.Add(myInt);        
  }

  // display the min / max vals only if there was not an error
  int biggestNumber = numbers.Max();
  int smallestNumber = numbers.Min();
  Console.WriteLine("Your biggest number was: " + biggestNumber);
  Console.WriteLine("Your smallest number was: " + smallestNumber);

  int result = numbers.Sum() / numbers.Count();
  Console.WriteLine("The average of your numbers are: " + result);