好吧,所以我为一个小型学校项目制作了这个程序。这只是一个小型控制台应用程序,用户应该输入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等)执行此操作。所以我认为这可能是一种更简单的方法,而不是复制粘贴每个用户输入的代码块。
我的问题是:是否有一个更简单/更简化的解决方案来验证所有用户输入(可能同时验证)?
编辑:如果你能帮助我,我很想看看你将如何在我的代码中实现你的解决方案:)
你需要这样的东西,伙计。收集所有值到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();
这只是一个想法,基本上是循环直到用户输入正确的数字十次。当输入正确时,数字保存在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
}
我会使用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);