C# 数组最大值与最小值

来源:互联网 发布:nginx 线程模型 编辑:程序博客网 时间:2024/06/06 05:21
求最大值
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Max{    class Program    {        static void Main(string[] args)        {            int[]a={10,9,6,4234,312};//定义一个数组            int max = a[0];            for (int i = 0; i < a.Length; i++)//这里遍历数组            {                if (max<a[i])//判断每个数大小                {                    max = a[i];//最后这里等于最大值                }            }            Console.WriteLine(max);            Console.ReadKey();        }    }}


最小值

 static void Main(string[] args)        {                      int[] a = {10,1,5,6123,34};            int min = a[0];            for (int i = 0; i < a.Length; i++)            {                if (min>a[i])                {                    min = a[i];                }            }            Console.WriteLine(min);            Console.ReadKey();


0 0
原创粉丝点击