在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
C# 查找数组中指定数字,最小值,最大值。
1 class Program 2 { 3 static void Main(string[] args) 4 { 5 int searchNumber; 6 bool found; 7 //TestArray nums = new TestArray(10); 8 int[] nums = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; 9 #region 初始化数组 10 //Random rnd = new Random(100); 11 //for (int num = 0; num < 10; num++) 12 //{ 13 // nums.Insert(rnd.Next(0, 100)); 14 //} 15 #endregion 16 Console.WriteLine("Enter a num to search for:"); 17 searchNumber = Convert.ToInt32(Console.ReadLine()); 18 found = NumSearch(nums,searchNumber); 19 if (found) 20 { 21 Console.WriteLine(searchNumber + "is in the array."); 22 } 23 else 24 { 25 Console.WriteLine(searchNumber + "is not in the array."); 26 } 27 //查找数组中最小值函数 28 int min=FindMin(nums); 29 int max = FindMax(nums); 30 Console.WriteLine(min.ToString()); 31 Console.WriteLine(max.ToString()); 32 #region 排序实现 33 //Console.WriteLine("Before Sorting: "); 34 //nums.DisplayElements(); 35 //Console.WriteLine("Durring Sorting: "); 36 //nums.InsertionSort(); 37 //Console.WriteLine("After Sorting: "); 38 //nums.DisplayElements(); 39 #endregion 40 Console.ReadLine(); 41 } 42 //<summary> 43 //查找数组中制定数字的方法 44 //<param name="arr">数组</param> 45 //<param name="value">数字</param> 46 //</summary> 47 //<returns>bool</returns> 48 public static bool NumSearch(int [] arr,int value) 49 { 50 for (int index = 0; index < arr.Length; index++) 51 { 52 if (arr[index] == value) 53 { 54 return true; 55 } 56 } 57 return false; 58 } 59 //<summary> 60 //查找数组中最小数字的方法 61 //<param name="arr">数组</param> 62 //</summary> 63 //<returns>bool</returns> 64 public static int FindMin(int[] arr) 65 { 66 int min = arr[0]; 67 for (int index = 1; index < arr.Length; index++) 68 { 69 if (arr[index] < min) 70 { 71 min=arr[index]; 72 } 73 } 74 return min; 75 } 76 //<summary> 77 //查找数组中最大数字的方法 78 //<param name="arr">数组</param> 79 //</summary> 80 //<returns>bool</returns> 81 public static int FindMax(int[] arr) 82 { 83 int max = arr[0]; 84 for (int index = 1; index < arr.Length; index++) 85 { 86 if (arr[index] > max) 87 { 88 max = arr[index]; 89 } 90 } 91 return max; 92 } 93 94 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论