如何在不同方法中输入最小值,最大值和平均值的命令

编程入门 行业动态 更新时间:2024-10-18 06:07:54
本文介绍了如何在不同方法中输入最小值,最大值和平均值的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

如何在不同方法中输入最小值,最大值和平均值的命令

本文发布于:2023-11-30 04:27:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最大值   最小值   均值   命令   和平

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!