C#和.Ne学习
练习:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 /* 12 * 用方法来实现: 13 * 有一个字符串数组{"马龙","迈克尔乔丹","雷吉米勒","蒂姆驰肯","科比布莱恩特"} 14 * 计算出最长的字符串 15 */ 16 static void Main(string[] args) 17 { 18 string[] name = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆驰肯", "科比布莱恩特" }; 19 Console.WriteLine(Max(name)); 20 Console.ReadKey(); 21 } 22 /// <summary> 23 /// 返回一个字符串数组中最长的字符串 24 /// </summary> 25 /// <param name="name">接收一个字符串数组</param> 26 /// <returns>返回那个最长的字符串</returns> 27 public static string Max(string[] name) 28 { 29 int a = 0; 30 for (int t = 1; t < name.Length; ++t) 31 { 32 if (name[a].Length < name[t].Length) 33 { 34 a = t; 35 } 36 } 37 38 return name[a]; 39 } 40 } 41 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication2 8 { 9 class Program 10 { 11 /* 12 * 用方法来实现: 13 * 请计算出一个整型数组的平均值保留两位小数 14 */ 15 static void Main(string[] args) 16 { 17 int[] a = {2, 98, 1, 83, 9, 71, 66}; 18 19 Console.WriteLine("平均值为:{0:f2}",Program.GetAverage(a)); 20 Console.ReadKey(); 21 } 22 /// <summary> 23 /// 返回一个整型数组的平均值,保留两位小数 24 /// </summary> 25 /// <param name="a">接收要计算的数组</param> 26 /// <returns>返回这个数组的平均值</returns> 27 public static double GetAverage(int[] a) 28 { 29 double average; 30 int sum = 0; 31 32 for (int t = 0; t < a.Length; ++t) 33 { 34 sum += a[t]; 35 } 36 average = sum / a.Length * 1.00; 37 38 return average; 39 } 40 } 41 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication4 8 { 9 class Program 10 { 11 /* 12 * 写一个方法,用来判断用户输入的数字是不是质数 13 * 再写一个方法,要求用户只能输入数字 输入有误就一直让用户输入 14 */ 15 static void Main(string[] args) 16 { 17 int a; 18 19 Console.Write("请输入一个数:"); 20 Program.JudgeNumber(out a); 21 22 Console.WriteLine(Program.PrimeNumber(a)); 23 Console.ReadKey(); 24 } 25 /// <summary> 26 /// 判断一个数是否是质数 27 /// </summary> 28 /// <param name="a">接收一个要判断的数</param> 29 /// <returns>返回判断结果</returns> 30 public static bool PrimeNumber(int a) 31 { 32 if(a<2) 33 { 34 return false; 35 } 36 else 37 { 38 for (int t = 2; t <= a / 2; ++t) 39 { 40 if (0 == a % t) 41 { 42 return false; 43 } 44 } 45 return true; 46 } 47 } 48 public static void JudgeNumber(out int a) 49 { 50 while (true) 51 { 52 try 53 { 54 a = Convert.ToInt32(Console.ReadLine()); 55 break; 56 } 57 catch (Exception ex) 58 { 59 Console.WriteLine(ex); 60 Console.Write("请重新输入:"); 61 } 62 } 63 return; 64 } 65 } 66 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 /* 12 * 收受输入后判断其等级并显示出来。 13 * 判断依据如下,等级= 14 * 优(90~100)良(80~89)中(70~79)及格(60~69)差(0~59) 15 */ 16 static void Main(string[] args) 17 { 18 double grade; 19 20 Console.Write("请输入一个成绩:"); 21 try 22 { 23 grade = Convert.ToDouble(Console.ReadLine()); 24 Console.WriteLine(JudgeGrade(grade)); 25 } 26 catch(Exception ex) 27 { 28 Console.WriteLine(ex); 29 } 30 31 Console.ReadKey(); 32 } 33 public static string JudgeGrade(double a) 34 { 35 if (90 <= a && 100 >= a ) 36 { 37 return "优"; 38 } 39 else if (80 <= a && 89 >= a) 40 { 41 return "良"; 42 } 43 else if (70 <= a && 79 >= a) 44 { 45 return "中"; 46 } 47 else if (60 <= a && 69 >= a) 48 { 49 return "及格"; 50 } 51 else 52 { 53 return "差"; 54 } 55 } 56 } 57 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication2 8 { 9 class Program 10 { 11 /* 12 * 将字符串数组{"中国","美国","巴西","澳大利亚","加拿大"}的内容反转 13 */ 14 static void Main(string[] args) 15 { 16 string[] str = { "中国", "美国", "巴西", "澳大利亚", "加拿大" }; 17 Program.Reverse(str); 18 19 20 for (int t = 0; t < str.Length; ++t) 21 { 22 Console.Write(str[t]+" "); 23 } 24 Console.ReadKey(); 25 26 } 27 public static void Reverse(string[] str) 28 { 29 for (int t1 = 0,t2=str.Length-1; t1<t2 ; ++t1,--t2) 30 { 31 string temp = str[t1]; 32 str[t1] = str[t2]; 33 str[t2] = temp; 34 } 35 return; 36 } 37 } 38 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 8 namespace ConsoleApplication1 9 { 10 11 class Program 12 { 13 const double PI = 3.1415; 14 static void Main(string[] args) 15 { 16 double r = 9; 17 double perimeter = Program.Perimeter(r); 18 double area = Program.Area(r); 19 20 Console.WriteLine("周长:{0:0.00}面积:{1:0.00}", perimeter, area); 21 Console.ReadKey(); 22 } 23 /// <summary> 24 /// 计算周长 25 /// </summary> 26 /// <param name="r">传入半径</param> 27 /// <returns>返回周长</returns> 28 public static double Perimeter(double r) 29 { 30 double perimeter = 2 * r * PI; 31 32 return perimeter; 33 } 34 /// <summary> 35 /// 计算面积 36 /// </summary> 37 /// <param name="r">传入半径</param> 38 /// <returns>返回面积</returns> 39 public static double Area(double r) 40 { 41 double area = r * r * PI; 42 43 return area; 44 } 45 46 } 47 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 8 namespace ConsoleApplication1 9 { 10 11 class Program 12 { 13 const double PI = 3.1415; 14 static void Main(string[] args) 15 { 16 double r = 9; 17 double perimeter = Program.Perimeter(r); 18 double area = Program.Area(r); 19 20 Console.WriteLine("周长:{0:0.00}面积:{1:0.00}", perimeter, area); 21 Console.ReadKey(); 22 } 23 /// <summary> 24 /// 计算周长 25 /// </summary> 26 /// <param name="r">传入半径</param> 27 /// <returns>返回周长</returns> 28 public static double Perimeter(double r) 29 { 30 double perimeter = 2 * r * PI; 31 32 return perimeter; 33 } 34 /// <summary> 35 /// 计算面积 36 /// </summary> 37 /// <param name="r">传入半径</param> 38 /// <returns>返回面积</returns> 39 public static double Area(double r) 40 { 41 double area = r * r * PI; 42 43 return area; 44 } 45 46 } 47 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 /* 12 * 计算任意多个数的最大值(提示:params) 13 */ 14 static void Main(string[] args) 15 { 16 Console.WriteLine(Program.GetMax(2, 8, 9, 1, 14, 12).ToString()); 17 } 18 /// <summary> 19 /// 返回一个整型数组的最大值 20 /// </summary> 21 /// <param name="a">接收数据的整型数组</param> 22 /// <returns>返回数组中最大值</returns> 23 public static int GetMax(params int[] a) 24 { 25 int max = a[0]; 26 for (int t = 1; t < a.Length - 1; ++t) 27 { 28 if (max < a[t]) 29 { 30 max = a[t]; 31 } 32 } 33 34 return max; 35 } 36 } 37 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication2 8 { 9 class Program 10 { 11 /* 12 * 计算通过冒泡排序法对整型数组{1, 3, 5, 7, 90, 2, 4, 6, 8, 10}实现升序排序 13 */ 14 static void Main(string[] args) 15 { 16 int[] a = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }; 17 Program.ArraySort(a); 18 19 Program.ArrayShow(a); 20 21 Console.ReadKey(); 22 } 23 /// <summary> 24 /// 为一个整型数组排序 25 /// </summary> 26 /// <param name="a">要排序的数组</param> 27 public static void ArraySort(int[] a) 28 { 29 for (int i = 0; i < a.Length - 1; ++i) 30 { 31 for (int j = i + 1; j <= a.Length - 1; ++j) 32 { 33 if (a[i] > a[j]) 34 { 35 a[i] = a[i]+a[j]; 36 a[j] = a[i]-a[j]; 37 a[i] = a[i]-a[j]; 38 } 39 } 40 } 41 42 return; 43 } 44 /// <summary> 45 ///控制台输出一个数组的每个元素 46 /// </summary> 47 /// <param name="a">要输入的数组</param> 48 public static void ArrayShow(int[] a) 49 { 50 for (int t = 0; t <= a.Length - 1; ++t) 51 { 52 Console.Write(a[t]); 53 Console.Write(" "); 54 } 55 Console.Write("\n"); 56 57 return; 58 } 59 } 60 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication3 8 { 9 class Program 10 { 11 /* 12 * 用方法实现,请计算出一个整型数组的平均值 13 * {1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }. 14 * 要求计算结果如果有小数保留后两位(四舍五入) 15 */ 16 static void Main(string[] args) 17 { 18 int[] a = { 1, 3, 5, 7, 93, 33, 4, 4, 6, 8, 10 }; 19 20 Console.WriteLine("{0:0.00}", ArrayAverage(a)); 21 Console.ReadKey(); 22 } 23 /// <summary> 24 /// 返回一个整型数组的平均值 25 /// 保留后两位小数(四舍五入) 26 /// </summary> 27 /// <param name="a">计算平均值的数组</param> 28 /// <returns>返回数组的平均值</returns> 29 public static double ArrayAverage(int[] a) 30 { 31 double average = 0; 32 for (int t = 0; t < a.Length; ++t) 33 { 34 average +=a[t]; 35 } 36 average = average * 1.000 / a.Length; 37 38 return average; 39 } 40 } 41 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 /* 12 * 将一个字符串数组输出为|分割的形式,比如“梅西|卡卡|郑大世” 13 */ 14 static void Main(string[] args) 15 { 16 string[] str = { "梅西", "卡卡", "郑大世" }; 17 18 Program.StringShow(ref str); 19 20 21 Console.ReadKey(); 22 } 23 /// <summary> 24 /// 字符串用|分割输出 25 /// </summary> 26 /// <param name="str">要分割输出的字符串</param> 27 public static void StringShow(ref string[] str) 28 { 29 for (int t = 0; t < str.Length; ++t) 30 { 31 Console.Write(str[t]); 32 if (t != str.Length - 1) 33 { 34 Console.Write("|"); 35 } 36 } 37 Console.Write("\n"); 38 } 39 } 40 }