枚举类型:
一组常量的组合
在不指定任何索引的情况下,默认第一个字段从0开始,之后的依次+1 在指定了某个索引的情况下,之后的字段索引会依次+1 若之前定义的一个字段的索引指向了之后的某个默认字段,那么,他俩完全相同 //若定义枚举类型时,下面有个字段等于之前的一个字段,那么就是等于这个字段 Console.WriteLine(meiju.four); //若等于之前的一个常量字段的时候强制取int,那就是等于的这个字段的索引号 Console.WriteLine((int)meiju.four);定义方法:
enum meiju:int { one=3, two, three, four=three }
Console.WriteLine(meiju.one); Console.WriteLine(meiju.three); //在不取int类型的时候直接打印,出来的是常量字段 Console.WriteLine(meiju.one); //在取int类型的时候直接打印,出来的是这个字段代表的值 Console.WriteLine((int)meiju.one); //若定义枚举类型时,下面有个字段等于之前的一个字段,那么就是等于这个字段 Console.WriteLine(meiju.four); //若等于之前的一个常量字段的时候强制取int,那就是等于的这个字段的索引号 Console.WriteLine((int)meiju.four); //不管第一个常量字段指定的索引从几开始,那么之后的所有常量的代表值会自动+1 Console.WriteLine(meiju.two); Console.WriteLine((int)meiju.two); Console.WriteLine((int)meiju.three); Console.WriteLine((int)meiju.four); Console.WriteLine(meiju.one); Console.WriteLine((int)meiju.one); Console.WriteLine((int)meiju.two); Console.WriteLine((int )meiju.three);
递归:递归是指在函数里面,调用这个函数本身
例子:求1~N的阶乘
public int Jie(int a) { int sum; if (a == 1) { return a; } sum = a * Jie(a - 1); return sum; }
Program hanshu = new Program(); int sum = hanshu.Jie(5); Console.WriteLine(sum);
题目1:猴子摘了好多好多桃子,一天需要吃掉总数的1/3,觉得不过瘾,还得再多吃一个吃到第9天的时候,发现只有2个桃子了。问,当初猴子摘了多少桃子
public double Tao(double a) { double sum; if (a == 9) { return 2; } sum = 1.5 * (Tao(a + 1)+1); return sum; }
Program hs = new Program(); double sum = hs.Tao(1); Console.WriteLine(sum);
结构体类型题目积累:
定义一个结构体,存放关于车辆的几个信息
将所有车的信息都放入集合中 车型号 价格(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 81所有在售车辆中最便宜的
2求宝马中最便宜的车型是什么,所有信息列出3选个轴距最长的 4选个轴距最长的,必须是奔驰5想要一辆油耗最低的车 6想要一辆油耗最低的奥迪车7我只有50万,看看能买什么车 8我只需要60万以上的车,列出来所有车型和所有信息 9轴距必须超过3m,列列表 10油耗在8.5以下都行,列列表11我不在乎油耗,要油耗最高的车Car car1 = new Car(); car1.name = "宝马320Li"; car1.money = 38; car1.mm = 2920; car1.pailiang = 6.9; Car car2 = new Car(); car2.name = "宝马520Li"; car2.money = 43; car2.mm = 3108; car2.pailiang = 7.2; Car car3 = new Car(); car3.name = "宝马730Li"; car3.money = 89; car3.mm = 3210; car3.pailiang = 6.3; Car car4 = new Car(); car4.name = "奥迪A4L35TFSI"; car4.money = 31; car4.mm = 2869; car4.pailiang = 6.2; Car car5 = new Car(); car5.name = "奥迪A6L30TFSI"; car5.money = 43; car5.mm = 3012; car5.pailiang = 7.6; Car car6 = new Car(); car6.name = "奥迪A8L45TFSI"; car6.money = 89; car6.mm = 3122; car6.pailiang = 8.1; Car car7 = new Car(); car7.name = "奔驰C200L"; car7.money = 35; car7.mm = 2920; car7.pailiang = 6.1; Car car8 = new Car(); car8.name = "奔驰E260L"; car8.money = 48; car8.mm = 3014; car8.pailiang = 6.7; Car car9 = new Car(); car9.name = "奔驰S320L"; car9.money = 93; car9.mm = 3165; car9.pailiang = 8; Car[] ww = new Car[] { car1, car2, car3, car4, car5, car6, car7, car8, car9 }; Console.WriteLine("请问你们这里宝马中最便宜的车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].money > ww[j].money) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } for (int i = 0; i < 9; i++) { if (ww[i].name.Contains("宝马")) { Console.WriteLine("\t\t价格最低的宝马车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); break; } } Console.WriteLine(); Console.WriteLine("请问你们这里轴距最长的奔驰车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].mm < ww[j].mm) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } for (int i = 0; i < 9; i++) { if (ww[i].name.Contains("奔驰")) { Console.WriteLine("\t\t轴距最长的奔驰车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); break; } } Console.WriteLine(); Console.WriteLine("请问你们这里油耗最少的奥迪车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].pailiang > ww[j].pailiang) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } for (int i = 0; i < 9; i++) { if (ww[i].name.Contains("奥迪")) { Console.WriteLine("\t\t油耗最少的奥迪车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); break; } } Console.WriteLine(); Console.WriteLine("油耗在8.5以下能买什么车:"); Console.WriteLine("油耗在8.5以下的车分别有:"); for (int i = 0; i < 9; i++) { if (ww[i].pailiang < 8.5) { Console.WriteLine("\t\t{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); } } Console.WriteLine(); Console.WriteLine("我只有50万,看看能买什么车:"); Console.WriteLine("低于50万的车分别有:"); for (int i = 0; i < 9; i++) { if (ww[i].money < 50) { Console.WriteLine("\t\t{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); } } Console.WriteLine(); Console.WriteLine("轴距必须超过3m的车都有那些"); Console.WriteLine("轴距超过3m的车分别有:"); for (int i = 0; i < 9; i++) { if (ww[i].mm > 3000) { Console.WriteLine("\t\t{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); } } Console.WriteLine(); Console.WriteLine("我只需要60万以上的车,列出来所有车型和所有信息:"); Console.WriteLine("60万以上的车分别有:"); for (int i = 0; i < 9; i++) { if (ww[i].money > 60) { Console.WriteLine("\t\t{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[i].name, ww[i].money, ww[i].mm, ww[i].pailiang); } } Console.WriteLine(); Console.WriteLine("请问你们这里最便宜的车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].money < ww[j].money) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } Console.WriteLine("\t\t最便宜的这辆车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[8].name, ww[8].money, ww[8].mm, ww[8].pailiang); Console.WriteLine(); Console.WriteLine("请问你们这里轴距最长的车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].mm > ww[j].mm) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } Console.WriteLine("\t\t轴距最长的这辆车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[8].name, ww[8].money, ww[8].mm, ww[8].pailiang); Console.WriteLine(); Console.WriteLine("请问你们这里油耗最多的车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].pailiang > ww[j].pailiang) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } Console.WriteLine("\t\t油耗最多的这辆车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[8].name, ww[8].money, ww[8].mm, ww[8].pailiang); Console.WriteLine(); Console.WriteLine("请问你们这里油耗最少的车是哪辆:"); for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (ww[i].pailiang < ww[j].pailiang) { Car zong = ww[i]; ww[i] = ww[j]; ww[j] = zong; } } } Console.WriteLine("\t\t油耗最少的这辆车是{0},价格是{1}万元,轴距{2}mm,排量是{3}毫升", ww[8].name, ww[8].money, ww[8].mm, ww[8].pailiang); Console.WriteLine();