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

时间:2016-01-07 20:39:45

标签: c# validation console-application user-input numeric

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

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等)执行此操作。所以我想也许这是一种更简单的方法,而不是为每个用户输入复制粘贴代码块。

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

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

3 个答案:

答案 0 :(得分:1)

你需要这样的东西,伙计。将所有值收集到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();

答案 1 :(得分:0)

这只是一个想法,基本上你循环直到用户输入十次正确的数字。输入正确后,该号码将保存在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
}

答案 2 :(得分:0)

我会使用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);