C# Arraylist + struct 综合练习 枚举ENUE 递归
Posted 张凯::get("新技能")
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# Arraylist + struct 综合练习 枚举ENUE 递归相关的知识,希望对你有一定的参考价值。
枚举类型
一组常量的组合,
在不制定任何索引的情况下,默认第一个字段从0开始,之后的依次+1 在指定了某个索引的情况下,之后的依次+1 若之前定义的某字段的索引指向了之后的某个默认字段,那么他俩完全相同
不需要初始化
Console.WriteLine(meiju.five);
//常量字段 Console.WriteLine((int)meiju.five);
//字段代表的值 Console.WriteLine(meiju.one);
//常量字段 Console.WriteLine((int)meiju.one);
//字段代表的值 Console.WriteLine(meiju.four);
//定义枚举类型时,等于之前的一个字段,那么就是等于那个字段
//定义枚举类型时,等于一个int的时候,他和他后面的索引+1
1,
Arraylist + struct 综合练习 数据的存放 数据的 提取 提取时注意格式转变 初始化
//定义一个结构体,存放关于车辆的几个信息
//将所有车的信息都放入集合中
//车型号 价格(W) 轴距 (mm) 油耗(L/100km)
//宝马320Li 38 2920 6.9
//宝马520Li 43 3108 7.2
//宝马730Li 89 3210 6.3
//奥迪A4L35TFSI 31 2869 6.2
//奥迪A6L30TFSI 43 3012 7.6
//奥迪A8L45TFSI 89 3122 8.1
//奔驰C200L 35 2920 6.1
//奔驰E260L 48 3014 6.7
//奔驰S320L 93 3165 8
1 class Program 2 { 3 struct Car 4 { 5 public string name; 6 public double price; 7 public double zhou; 8 public double you; 9 } 10 11 static void Main(string[] args) 12 { 13 14 #region 购车例题 15 ArrayList caral=new ArrayList(); 16 Car a1=new Car(); 17 a1.name="宝马320Li"; 18 a1.price=38; 19 a1.zhou=2920; 20 a1.you=6.9; 21 caral.Add(a1); 22 Car a2=new Car(); 23 a2.name="宝马520Li"; 24 a2.price=43; 25 a2.zhou=3108; 26 a2.you=7.2; 27 caral.Add(a2); 28 29 Car a3=new Car(); 30 a3.name="宝马730Li"; 31 a3.price=89; 32 a3.zhou=3210; 33 a3.you=6.3; 34 caral.Add(a3); 35 36 Car a4=new Car(); 37 a4.name="奥迪A4L35TFSI"; 38 a4.price=31; 39 a4.zhou=2869; 40 a4.you=6.2; 41 caral.Add(a4); 42 43 Car a5=new Car(); 44 a5.name="奥迪A6L30TFSI"; 45 a5.price=43; 46 a5.zhou=3012; 47 a5.you=7.6; 48 caral.Add(a5); 49 50 Car a6=new Car(); 51 a6.name="奥迪A8L45TFSI"; 52 a6.price=89; 53 a6.zhou=3122; 54 a6.you=8.1; 55 caral.Add(a6); 56 57 Car a7=new Car(); 58 a7.name="奔驰C200L"; 59 a7.price=35; 60 a7.zhou=2920; 61 a7.you=6.1; 62 caral.Add(a7); 63 64 Car a8=new Car(); 65 a8.name="奔驰E260L"; 66 a8.price=48; 67 a8.zhou=3014; 68 a8.you=6.7; 69 caral.Add(a8); 70 71 Car a9=new Car(); 72 a9.name="奔驰S320L"; 73 a9.price=93; 74 a9.zhou=3165; 75 a9.you=8; 76 caral.Add(a9); 77 //所有在售车辆中最便宜的 78 for (int i = 0; i < 8;i++ ) 79 { 80 for (int j = i + 1; j < 9;j++ ) 81 { 82 Car q1 =(Car)caral[i]; 83 Car q2 = (Car)caral[j]; 84 if(q1.price>q2.price) 85 { 86 object zhong = caral[i]; 87 caral[i] = caral[j]; 88 caral[j] = zhong; 89 } 90 } 91 } 92 Car cheap = (Car)caral[0]; 93 Console.WriteLine("最便宜的是{0},价格{1},轴距{2},油耗{3}。",cheap.name,cheap.price,cheap.zhou,cheap.you); 94 95 96 Console.WriteLine("---------------------------------------------------------------------------------------------"); 97 98 99 //求宝马中最便宜的车型是什么,所有信息列出 100 for (int i = 0; i < 9;i++ ) 101 { 102 Car baocheap = (Car)caral[i]; 103 if(baocheap.name.Contains("宝马")) 104 { 105 Console.WriteLine("宝马车中最便宜的是{0},价格{1},轴距{2},油耗{3}。", baocheap.name, baocheap.price, baocheap.zhou, baocheap.you); 106 break; 107 } 108 } 109 Console.WriteLine("---------------------------------------------------------------------------------------------"); 110 111 //选个轴距最长的 112 for (int i = 0; i < 8; i++) 113 { 114 for (int j = i + 1; j < 9; j++) 115 { 116 Car q1 = (Car)caral[i]; 117 Car q2 = (Car)caral[j]; 118 if (q1.zhou < q2.zhou) 119 { 120 object zhong = caral[i]; 121 caral[i] = caral[j]; 122 caral[j] = zhong; 123 } 124 } 125 } 126 Car zhou = (Car)caral[0]; 127 Console.WriteLine("轴距最长的车是{0},价格{1},轴距{2},油耗{3}。", zhou.name, zhou.price, zhou.zhou, zhou.you); 128 Console.WriteLine("---------------------------------------------------------------------------------------------"); 129 130 131 //选个轴距最长的,必须是奔驰 132 for (int i = 0; i < 9; i++) 133 { 134 Car zhouben = (Car)caral[i]; 135 if (zhouben.name.Contains("奔驰")) 136 { 137 Console.WriteLine("奔驰车中轴距最长的是{0},价格{1},轴距{2},油耗{3}。", zhouben.name, zhouben.price, zhouben.zhou, zhouben.you); 138 break; 139 } 140 } 141 Console.WriteLine("---------------------------------------------------------------------------------------------"); 142 143 144 145 //想要一辆油耗最低的车 146 for (int i = 0; i < 8; i++) 147 { 148 for (int j = i + 1; j < 9; j++) 149 { 150 Car q1 = (Car)caral[i]; 151 Car q2 = (Car)caral[j]; 152 if (q1.you > q2.you) 153 { 154 object zhong = caral[i]; 155 caral[i] = caral[j]; 156 caral[j] = zhong; 157 } 158 } 159 } 160 Car youdi = (Car)caral[0]; 161 Console.WriteLine("油耗最低的车辆是{0},价格{1},轴距{2},油耗{3}。", youdi.name, youdi.price, youdi.zhou, youdi.you); 162 Console.WriteLine("---------------------------------------------------------------------------------------------"); 163 164 165 166 //想要一辆油耗最低的奥迪车 167 for (int i = 0; i < 9; i++) 168 { 169 Car zhouben = (Car)caral[i]; 170 if (zhouben.name.Contains("奥迪")) 171 { 172 Console.WriteLine("奥迪车中油耗最低的是{0},价格{1},轴距{2},油耗{3}。", zhouben.name, zhouben.price, zhouben.zhou, zhouben.you); 173 break; 174 } 175 } 176 Console.WriteLine("---------------------------------------------------------------------------------------------"); 177 178 179 //我只有50万,看看能买什么车 180 int biao = 0; 181 for (int i = 0; i < 9;i++ ) 182 { 183 Car diwushi = (Car)caral[i]; 184 if(diwushi.price<=50) 185 { 186 biao++; 187 Console.WriteLine("第{0}辆:{1},价格{2},轴距{3},油耗{4}。",biao,diwushi.name,diwushi.price,diwushi.zhou,diwushi.you); 188 } 189 } 190 Console.WriteLine("---------------------------------------------------------------------------------------------"); 191 192 //我只需要60万以上的车,列出来所有车型和所有信息 193 int biao1 = 0; 194 for (int i = 0; i < 9; i++) 195 { 196 Car diwushi = (Car)caral[i]; 197 if (diwushi.price > 60) 198 { 199 biao1++; 200 Console.WriteLine("第{0}辆:{1},价格{2},轴距{3},油耗{4}。", biao1, diwushi.name, diwushi.price, diwushi.zhou, diwushi.you); 201 } 202 } 203 Console.WriteLine("---------------------------------------------------------------------------------------------"); 204 205 206 207 //轴距必须超过3m,列列表 208 int biao2 = 0; 209 for (int i = 0; i < 9; i++) 210 { 211 Car diwushi = (Car)caral[i]; 212 if (diwushi.zhou>3000) 213 { 214 biao2++; 215 Console.WriteLine("第{0}辆:{1},价格{2},轴距{3},油耗{4}。", biao2, diwushi.name, diwushi.price, diwushi.zhou, diwushi.you); 216 } 217 } 218 Console.WriteLine("---------------------------------------------------------------------------------------------"); 219 220 //油耗在8.5以下都行,列列表 221 int biao3 = 0; 222 for (int i = 0; i < 9; i++) 223 { 224 Car diwushi = (Car)caral[i]; 225 if (diwushi.you<=8.5) 226 { 227 biao3++; 228 Console.WriteLine("第{0}辆:{1},价格{2},轴距{3},油耗{4}。", biao3, diwushi.name, diwushi.price, diwushi.zhou, diwushi.you); 229 } 230 } 231 Console.WriteLine("---------------------------------------------------------------------------------------------"); 232 233 234 235 //我不在乎油耗,要油耗最高的车 236 for (int i = 0; i < 8; i++) 237 { 238 for (int j = i + 1; j < 9; j++) 239 { 240 Car q1 = (Car)caral[i]; 241 Car q2 = (Car)caral[j]; 242 if (q1.you < q2.you) 243 { 244 object zhong = caral[i]; 245 caral[i] = caral[j]; 246 caral[j] = zhong; 247 } 248 } 249 } 250 Car yougao = (Car)caral[0]; 251 Console.WriteLine("油耗最高的车辆是{0},价格{1},轴距{2},油耗{3}。", yougao.name, yougao.price, yougao.zhou, yougao.you); 252 253 #endregion
2.
public ArrayList al = new ArrayList(); 注意位置
//输入班级人数,输入学生的学号、姓名、语数英分数
//存入结构体
//然后求语文分数为最高分的两个学生的所有信息
//数学最高分的两个学生的信息
//英语平均分
class Program { #region 成绩 //结构体是自定义类型,可以当做数据类型来使用 struct Student { public int code; public string name; public Score score; } struct Score { public double yufen; public double shufen; public double yingfen; } public ArrayList al = new ArrayList(); ******* static void Main(string[] args) { #region 班级排名 //输入班级人数,输入学生的学号、姓名、语数英分数 //存入结构体 //然后求语文分数为最高分的两个学生的所有信息 //数学最高分的两个学生的信息 //英语平均分 Console.Write("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); Program hs = new Program(); for (int i = 0; i < a;i++ ) { Student st = new Student(); Console.Write("请输入第{0}个学生的学号:",(i+1)); st.code = int.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的姓名:", (i + 1)); st.name = Console.ReadLine(); Console.Write("请输入第{0}个学生的语文分数:", (i + 1)); st.score.yufen = double.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的数学分数:", (i + 1)); st.score.shufen = double.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的英语分数:", (i + 1)); st.score.yingfen = double.Parse(Console.ReadLine()); hs.al.Add(st); } Console.WriteLine("所有人员信息输入完毕!请按回车键继续!"); Console.ReadLine(); //然后求语文分数为最高分的两个学生的所有信息 for (int i = 0; i < a - 1;i++ ) { for (int j = i + 1; j < a;j++ ) { Student st1 =(Student)hs.al[i]; Student st2 =(Student)hs.al[j]; if(st1.score.yufen<st2.score.yufen) { object zhong = hs.al[i]; hs.al[i] = hs.al[j]; hs.al[j] = zhong; } } } Student yu1 = (Student)hs.al[0]; Student yu2 = (Student)hs.al[1]; Console.WriteLine("语文分数最高的第一个学生的学号:{0},姓名:{1},语文分数:{2},数学分数:{3},英语分数:{4}。",yu1.code,yu1.name,yu1.score.yufen,yu1.score.shufen,yu1.score.yingfen); Console.WriteLine("语文分数最高的第二个学生的学号:{0},姓名:{1},语文分数:{2},数学分数:{3},英语分数:{4}。", yu2.code, yu2.name, yu2.score.yufen, yu2.score.shufen, yu2.score.yingfen); //数学最高分的两个学生的信息 for (int i = 0; i < a - 1; i++) { for (int j = i + 1; j < a; j++) { Student st1 = (Student)hs.al[i]; Student st2 = (Student)hs.al[j]; if (st1.score.shufen < st2.score.shufen) { object zhong = hs.al[i]; hs.al[i] = hs.al[j]; hs.al[j] = zhong; } } } Student shu1 = (Student)hs.al[0]; Student shu2 = (Student)hs.al[1]; Console.WriteLine("数学分数最高的第一个学生的学号:{0},姓名:{1},语文分数:{2},数学分数:{3},英语分数:{4}。", shu1.code, shu1.name, shu1.score.yufen, shu1.score.shufen, shu1.score.yingfen); Console.WriteLine("数学分数最高的第二个学生的学号:{0},姓名:{1},语文分数:{2},数学分数:{3},英语分数:{4}。", shu2.code, shu2.name, shu2.score.yufen, shu2.score.shufen, shu2.score.yingfen); //英语平均分 double sum = 0; for (int i = 0; i < hs.al.Count;i++ ) { Student ying = (Student)hs.al[i]; sum += ying.score.yingfen; } Console.WriteLine("班级英语平均分是:"+(sum/a)); #endregion
3.递归
函数体内调用本函数自身,直到符合某一条件不再继续调用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace以上是关于C# Arraylist + struct 综合练习 枚举ENUE 递归的主要内容,如果未能解决你的问题,请参考以下文章