本文介绍了如何在不同方法中输入最小值,最大值和平均值的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System; using System.Linq; public class Test { public static void Main() { const int NUMBERS = 1000; Random myRandom = new Random(); int [] a = new int[NUMBERS]; int i = 0; int max = int.MinValue, min = int.MaxValue, sum = 0; while (i < NUMBERS) { a[i] = myRandom.Next(1, 501); if (max < a[i]) max = a[i]; if (min > a[i]) min = a[i]; sum += a[i]; ++i; } double avg = (double)sum / NUMBERS; Console.WriteLine("The minimum number is: {0}", min ); Console.WriteLine("The maximum number is: {0}", max); Console.WriteLine("The average of these numbers is: {0}", avg); int x = Convert.ToInt32(Console.ReadLine()); if(a.Contains(x)) { Console.WriteLine("Your number was one of the original 1000!"); } else { Console.WriteLine("Your number was not one of the original 1000"); } } }
推荐答案
为什么不简单地使用LINQ Min [ ^ ]和Max [ ^ ]方法。 [加法] 请考虑以下代码 Why not simply use LINQ Min[^] and Max[^] methods. [Addition] Consider the following code int[] myarray = new int[5] { 3, 5, 9, 2, 5 }; System.Diagnostics.Debug.WriteLine(string.Format("Minimum is {0}", myarray.Min())); System.Diagnostics.Debug.WriteLine(string.Format("Maximum is {0}", myarray.Max())); System.Diagnostics.Debug.WriteLine(string.Format("And somewhere between is {0}", myarray.Average()));
输出为
The output is
Minimum is 2 Maximum is 9 And somewhere between is 4,8
更多推荐
如何在不同方法中输入最小值,最大值和平均值的命令
发布评论