C#基础编程---数组最大值最小值

/*
   question :数组最大值最小值

*/
using System;
namespace TestApp
{
       class Test
       {   
               public int[] getArray(int size)    //产生一个数组
               {
                     int[] array = new int[size];
                     Console.WriteLine("产生的数组是: ");
                     Random random = new Random();
                     for(int i=1; i<=array.Length; i++)
                     {
                           array[i-1] = (int)(random.Next(0, 100)); //
                           Console.Write(array[i-1] + "/t");
                           if(i%4==0)Console.WriteLine();
                     }
                     Console.WriteLine("/n");
                     return array;      
               }
               public void calculate(int[] a)   //查找最大值和最小值
               {
                     int maxValue = 0, minValue = 0;
                     try{
                           maxValue = a[0];
                           minValue = a[0];
                           for(int i=0; i
                                 if(a[i]>maxValue)
                                    maxValue = a[i];
                                 if(a[i]
                                    minValue = a[i];
                           }
                     }catch(Exception  ex){
                          // ex.PrintStackTrace(Console.Write());
                          Console.WriteLine("发生错误!");
                     }
                     Console.WriteLine("maxValue={0}/t, minValue={1}", maxValue, minValue);
               }
               static void Main(string[] args)
               {
                     Test test = new Test();
                     int i = 1;
                     while(i > 0)
                     {
                           Console.Write("输入数组的长度: ");
                           i = Int32.Parse(Console.ReadLine());
                           int[] x = test.getArray(i);
                           test.calculate(x);
                     }
               }
       }
}

你可能感兴趣的:(C#)