C#求n个数中的最大值和最小值
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { Console.WriteLine("输入十个数: "); // int [] num = new int[10]; //for (int i = 0; i < 10; i++) //{ string s = Console.ReadLine(); // num[i] = int.Parse(s); // } string[] num = s.Split(' ');//把空格挖掉,把数存入数组 int[] m = new int[num.Length]; for (int i = 0; i < num.Length; i++) { m[i] = int.Parse(num[i]); } Console.WriteLine("最大值为: {0}",max(m,10)); Console.WriteLine("最小值为: {0}", min(m, 10)); Console.ReadKey(); } static int max(int[] num, int n) { int a = num[0]; for (int i = 0; i < n; i++) { if (a < num[i]) { a = num[i]; } } return a; } static int min(int []num,int n) { int a = num[0]; for (int i = 0; i < n; i++) { if (num[i] < a) { a = num[i]; } } return a; } } }