//定义一个结构体,存放关于车辆的几个信息 //将所有车的信息都放入集合中 //车型号 价格(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 //所有在售车辆中最便宜的 //求宝马中最便宜的车型是什么,所有信息列出 //选个轴距最长的 //选个轴距最长的,必须是奔驰 //想要一辆油耗最低的车 //想要一辆油耗最低的奥迪车 //我只有50万,看看能买什么车 //我只需要60万以上的车,列出来所有车型和所有信息 //轴距必须超过3m,列列表 //油耗在8.5以下都行,列列表 //我不在乎油耗,要油耗最高的车 ArrayList al = new ArrayList(); qiche qc = new qiche(); qc.xinghao = "宝马320Li"; qc.price = 38; qc.zhouju = 2920; qc.youhao = 6.9; al.Add(qc); qc.xinghao = "宝马520Li"; qc.price = 43; qc.zhouju = 3108; qc.youhao = 7.2; al.Add(qc); qc.xinghao = "宝马730Li"; qc.price = 89; qc.zhouju = 3210; qc.youhao = 6.3; al.Add(qc); qc.xinghao = "奥迪A4L35TFSI"; qc.price = 31; qc.zhouju = 2869; qc.youhao = 6.2; al.Add(qc); qc.xinghao = "奥迪A6L30TFSI"; qc.price = 43; qc.zhouju = 3012; qc.youhao = 7.6; al.Add(qc); qc.xinghao = "奥迪A8L45TFSI"; qc.price = 89; qc.zhouju = 3122; qc.youhao = 8.1; al.Add(qc); qc.xinghao = "奔驰C200L"; qc.price = 35; qc.zhouju = 2920; qc.youhao = 6.1; al.Add(qc); qc.xinghao = "奔驰E260L"; qc.price = 48; qc.zhouju = 3014; qc.youhao = 6.7; al.Add(qc); qc.xinghao = "奔驰S320L"; qc.price = 93; qc.zhouju = 3165; qc.youhao = 8; al.Add(qc); //所有在售车辆中最便宜的 for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[i]).price > ((qiche)al[j]).price) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } qiche qc1 = (qiche)al[0]; Console.WriteLine("所有在售车辆中最便宜的:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc1.xinghao, qc1.price, qc1.zhouju, qc1.youhao); Console.WriteLine(); //求宝马中最便宜的车型是什么,所有信息列出 for (int i = 0; i < 9; i++) { if (((qiche)al[i]).xinghao.Contains("宝马")) { qiche qc2 = (qiche)al[i]; Console.WriteLine("宝马车中最便宜的车辆信息:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc2.xinghao, qc2.price, qc2.zhouju, qc2.youhao); break; } } Console.WriteLine(); //选个轴距最长的 for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[i]).zhouju < ((qiche)al[j]).zhouju) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } qiche qc3 = (qiche)al[0]; Console.WriteLine("轴距最长的车辆信息:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc3.xinghao, qc3.price, qc3.zhouju, qc3.youhao); Console.WriteLine(); //选个轴距最长的,必须是奔驰 for (int i = 0; i < 9; i++) { if (((qiche)al[i]).xinghao.Contains("奔驰")) { qiche qc2 = (qiche)al[i]; Console.WriteLine("奔驰车中轴距最长的车辆信息:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc2.xinghao, qc2.price, qc2.zhouju, qc2.youhao); break; } } Console.WriteLine(); //想要一辆油耗最低的车 for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[i]).youhao > ((qiche)al[j]).youhao) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } qiche qc5 = (qiche)al[0]; Console.WriteLine("轴距最长的车辆信息:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc5.xinghao, qc5.price, qc5.zhouju, qc5.youhao); Console.WriteLine(); //想要一辆油耗最低的奥迪车 for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[j]).xinghao.Contains("奥迪")) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[i]).youhao > ((qiche)al[j]).youhao && ((qiche)al[i]).xinghao.Contains("奥迪") && ((qiche)al[j]).xinghao.Contains("奥迪")) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } qiche qc6 = (qiche)al[0]; Console.WriteLine("奥迪车中油耗最低的车辆信息:" + " " + "{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc6.xinghao, qc6.price, qc6.zhouju, qc6.youhao); Console.WriteLine(); //我只有50万,看看能买什么车 Console.WriteLine("只有50万,所有能买的车为:"); for (int i = 0; i < 9; i++) { if (((qiche)al[i]).price <= 50) { qiche qc7 = (qiche)al[i]; Console.WriteLine("{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc7.xinghao, qc7.price, qc7.zhouju, qc7.youhao); } } Console.WriteLine(); //我只需要60万以上的车,列出来所有车型和所有信息 Console.WriteLine("所有60万以上的车辆信息:"); for (int i = 0; i < 9; i++) { if (((qiche)al[i]).price > 60) { qiche qc7 = (qiche)al[i]; Console.WriteLine("{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc7.xinghao, qc7.price, qc7.zhouju, qc7.youhao); } } Console.WriteLine(); //轴距必须超过3m,列列表 Console.WriteLine("轴距超过3m的车辆信息:"); for (int i = 0; i < 9; i++) { if (((qiche)al[i]).zhouju > 3000) { qiche qc8 = (qiche)al[i]; Console.WriteLine("{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc8.xinghao, qc8.price, qc8.zhouju, qc8.youhao); } } Console.WriteLine(); //油耗在8.5以下都行,列列表 Console.WriteLine("油耗8.5以下的车辆信息:"); for (int i = 0; i < 9; i++) { if (((qiche)al[i]).youhao < 8.5) { qiche qc9 = (qiche)al[i]; Console.WriteLine("{0}" + " " + "价格:{1}万元" + " " + "轴距{2}mm" + " " + "油耗{3}L/100km。", qc9.xinghao, qc9.price, qc9.zhouju, qc9.youhao); } } Console.WriteLine(); //我不在乎油耗,要油耗最高的车 for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((qiche)al[i]).youhao < ((qiche)al[j]).youhao) { object zhong = al[i]; al[i] = al[j]; al[j] = zhong; } } } qiche qc10 = (qiche)al[0]; Console.WriteLine("油耗最高的车辆信息:{0},价格:{1}万元,轴距{2}mm,油耗{3}L/100km。", qc10.xinghao, qc10.price, qc10.zhouju, qc10.youhao);