传值、传址 结构体
传值:将变量名中存放的值进行传输 传址:将这个变量名直接传输过去, 若在另一边有赋值情况,这边的值会发生变化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void 传值(int a) 2 { 3 a += 10; 4 } 5 static void Main(string[] args) 6 { 7 //实例化 初始化 8 Program hs = new Program(); 9 int a = 0; 10 hs.传值(a);//a不变 11 }
将变量名中存放的值进行传输
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 private void 传址(int a, out int b) 2 { 3 b = a + 10; 4 } 5 static void Main(string[] args) 6 { 7 Program hs = new Program(); 8 int a = 0; 9 int b = 0; 10 hs.传址(a, out b);//b变 11 }
将这个变量名直接传输过去
out传址
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public void hanshu1(int a , out int b) 2 { 3 b = a + 10; 4 } 5 int a = 5; 6 int rr; 7 hanshu.hanshu1(a,out rr); 8 Console.WriteLine(rr);
如果需要函数返回多个值时,可以用减号拼接起来,返回后用split分割开
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public string Fanhui(string name , string sex,int age) 2 { 3 age += 10; 4 return name + "-"+sex +"-"+ age; 5 }
结构体
结构体:自定义类型 值类型一组变量的组合
定义位置 Class里面 main函数外面
结构体里面可以包含各种类型的数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 struct Student 2 { 3 public int xuehao; 4 public string name; 5 public string sex; 6 public Score score; 7 } 8 9 struct Score 10 { 11 public double yufen; 12 public double shufen; 13 public double yingfen; 14 } 15 static void Main(string[] args) 16 { 17 //实例化结构体 18 Student st = new Student(); 19 st.xuehao = 1001; 20 st.name = "张三"; 21 st.sex = "男"; 22 st.score = 33; 23 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 struct student 2 { 3 public int xuehao; 4 public string name; 5 public string xingbie; 6 public double fenshu; 7 } 8 static void Main(string[] args) 9 { 10 ArrayList al = new ArrayList(); 11 Console.Write("请输入班级人数"); 12 int a = int.Parse(Console.ReadLine()); 13 for (int i = 0; i < a;i++ ) 14 { 15 student sst = new student(); 16 Console.Write("请输入第{0}个学生的学号",(i+1)); 17 sst.xuehao = int.Parse(Console.ReadLine()); 18 Console.Write("请输入第{0}个学生的姓名", (i + 1)); 19 sst.name = Console.ReadLine(); 20 Console.Write("请输入第{0}个学生的性别", (i + 1)); 21 sst.xingbie = Console.ReadLine(); 22 Console.Write("请输入第{0}个学生的分数", (i + 1)); 23 sst.fenshu = double.Parse(Console.ReadLine()); 24 al.Add(sst); 25 } 26 Console.WriteLine("所有人员名单输入完毕请按回车开始打印"); 27 Console.ReadLine(); 28 for (int i = 0; i < a;i++ ) 29 { 30 student sst=(student)al[i]; 31 Console.WriteLine("第{0}个学生的学号是{1}姓名是{2}性别是{3}分数为{4}",(i+1),sst.xuehao,sst.name,sst.xingbie,sst.fenshu); 32 }
去超市选择要购买的商品
0.开始购买
1.洗发水 15元
2.牙刷 5元
3.可口可乐 3元
4.水杯 12元
5.毛巾 6元
6.结算
输入想要的物品编号,输入每种商品需要买几件
结算:需要列出来商品名称、单价、数量、
总价以及所有物品总价,结账时间
然后输入交给收银员的钱数,
不够,请继续交钱
够,结算,找钱
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Console.WriteLine("0.开始购买"); 2 //Console.WriteLine("1.洗发水 15元"); 3 //Console.WriteLine("2.牙刷 5元"); 4 //Console.WriteLine("3.可口可乐 3元"); 5 //Console.WriteLine("4.水杯 12元"); 6 //Console.WriteLine("5.毛巾 6元"); 7 Console.WriteLine("6.结算(退出)"); 8 9 Console.Write("请输入号码:"); 10 for (int i = 0; i == 0; ) 11 { 12 int aa = int.Parse(Console.ReadLine()); 13 if (aa == 0) 14 { 15 ArrayList al = new ArrayList(); 16 int biao1 = 0; 17 for (int j = 0; j == 0; ) 18 { 19 bool biaocuo = true; 20 string[] array = new string[3]; 21 Console.Clear(); 22 if (al.Count > 0) 23 { 24 for (int k = 0; k < al.Count; k++) 25 { 26 string[] yigou = (string[])al[k]; 27 Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。", yigou[0], yigou[1], yigou[2]); 28 } 29 } 30 Console.WriteLine("1.洗发水 15元"); 31 Console.WriteLine("2.牙刷 5元"); 32 Console.WriteLine("3.可口可乐 3元"); 33 Console.WriteLine("4.水杯 12元"); 34 Console.WriteLine("5.毛巾 6元"); 35 Console.WriteLine("6.结算"); 36 37 Console.Write("请输入选项:"); 38 int aaa = int.Parse(Console.ReadLine()); 39 switch (aaa) 40 { 41 case 1: 42 array[0] = "洗发水"; 43 array[1] = "15"; 44 biao1++; 45 Console.Write("您选择的是洗发水,请问您需要多少瓶?"); 46 break; 47 case 2: 48 array[0] = "牙刷"; 49 array[1] = "5"; 50 biao1++; 51 Console.Write("您选择的是牙刷,请问您需要多少支?"); 52 break; 53 case 3: 54 array[0] = "可口可乐"; 55 array[1] = "3"; 56 biao1++; 57 Console.Write("您选择的是可口可乐,请问您需要多少瓶?"); 58 break; 59 case 4: 60 array[0] = "水杯"; 61 array[1] = "12"; 62 biao1++; 63 Console.Write("您选择的是水杯,请问您需要多少个?"); 64 break; 65 case 5: 66 array[0] = "毛巾"; 67 array[1] = "6"; 68 biao1++; 69 Console.Write("您选择的是毛巾,请问您需要多少块?"); 70 break; 71 case 6: 72 if (biao1 == 0) 73 { 74 Console.Clear(); 75 Console.WriteLine("您什么也没有购买,您已经走出了超市。。。"); 76 j = 1; 77 i = 1; 78 } 79 else 80 { 81 int zong = 0; 82 for (int k = 0; k < al.Count; k++) 83 { 84 string[] yigou = (string[])al[k]; 85 Console.WriteLine("您选择了{0},单价为{1}元,数量为{2},单品总价:{3}。", yigou[0], yigou[1], yigou[2], (int.Parse(yigou[1]) * int.Parse(yigou[2]))); 86 zong += int.Parse(yigou[1]) * int.Parse(yigou[2]); 87 } 88 Console.Write("总价:" + zong + "元。请缴费:"); 89 int erjiao = 0; 90 for (int l = 0; l == 0; ) 91 { 92 int jiao = int.Parse(Console.ReadLine()); 93 jiao += erjiao; 94 if (jiao >= zong) 95 { 96 Console.WriteLine("交易成功,交易时间为:" + DateTime.Now); 97 Console.WriteLine("找零:" + (jiao - zong) + "元。"); 98 Console.WriteLine("谢谢惠顾!再见!"); 99 l = 1; 100 j = 1; 101 i = 1; 102 } 103 else 104 { 105 erjiao = jiao; 106 Console.Write("缴费金额不足,请继续缴费:"); 107 } 108 } 109 } 110 break; 111 default: 112 Console.WriteLine("查无此商品!请按回车键继续选择商品!"); 113 Console.ReadLine(); 114 biaocuo = false; 115 break; 116 } 117 if (i == 0 && j == 0 && biaocuo == true) 118 { 119 array[2] = Console.ReadLine(); 120 Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。请按回车键继续购买!", array[0], array[1], array[2]); 121 al.Add(array); 122 Console.ReadLine(); 123 } 124 } 125 } 126 else if (aa == 6) 127 { 128 Console.Clear(); 129 Console.WriteLine("您什么也没有购买!您已走出超市。。。"); 130 i = 1; 131 } 132 else 133 { 134 Console.Write("输入有误!请重新输入:"); 135 } 136 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 struct swe 2 {/* 3 public string chexing; 4 public int jiage; 5 public int mm; 6 public double l; 7 } 8 static void Main(string[] args) 9 { 10 ArrayList al = new ArrayList(); 11 swe swe10 = new swe(); 12 swe10.chexing = "宝马320Li "; 13 swe10.jiage = 38; 14 swe10.mm = 2920; 15 swe10.l = 6.9; 16 al.Add(swe10); 17 18 swe swe1 = new swe(); 19 swe1.chexing = "宝马520Li"; 20 swe1.jiage = 43; 21 swe1.mm = 3108; 22 swe1.l = 7.2; 23 al.Add(swe1); 24 25 swe swe2 = new swe(); 26 swe2.chexing = "宝马730Li "; 27 swe2.jiage = 89; 28 swe2.mm = 3210; 29 swe2.l = 6.3; 30 al.Add(swe2); 31 32 swe swe3 = new swe(); 33 swe3.chexing = "奥迪A4L35TFSI "; 34 swe3.jiage = 31; 35 swe3.mm = 2869; 36 swe3.l = 6.2; 37 al.Add(swe3); 38 39 swe swe4 = new swe(); 40 swe4.chexing = "奥迪A6L30TFSI"; 41 swe4.jiage = 43; 42 swe4.mm = 3012; 43 swe4.l = 7.6; 44 al.Add(swe4); 45 46 swe swe5 = new swe(); 47 swe5.chexing = "奥迪A8L45TFSI"; 48 swe5.jiage = 89; 49 swe5.mm = 3122; 50 swe5.l = 8.1; 51 al.Add(swe5); 52 53 swe swe6 = new swe(); 54 swe6.chexing = "奔驰C200L "; 55 swe6.jiage = 35; 56 swe6.mm = 2920; 57 swe6.l = 6.1; 58 al.Add(swe6); 59 60 swe swe7 = new swe(); 61 swe7.chexing = "奔驰E260L "; 62 swe7.jiage = 48; 63 swe7.mm = 3014; 64 swe7.l = 6.7; 65 al.Add(swe7); 66 67 swe swe8 = new swe(); 68 swe8.chexing = "奔驰S320L "; 69 swe8.jiage = 93; 70 swe8.mm = 3165; 71 swe8.l = 8; 72 al.Add(swe8); 73 74 for (int i = 0; i < 8; i++) 75 { 76 for (int j = i + 1; j < 9; j++) 77 { 78 if (((swe)al[i]).jiage < ((swe)al[j]).jiage) 79 { 80 object zhong = al[i]; 81 al[i] = al[j]; 82 al[j] = zhong; 83 } 84 } 85 } 86 //所有在售车辆中最便宜的 87 Console.WriteLine("在售车辆中最便宜的{0}", ((swe)al[8]).chexing); 88 Console.WriteLine("-----------------------------------------------------------------------------------------"); 89 90 //求宝马中最便宜的车型是什么,所有信息列出 91 ArrayList al1 = new ArrayList(); 92 for (int i = 0; i < 9; i++) 93 { 94 swe sk = new swe(); 95 sk = (swe)al[i]; 96 if (sk.chexing.Contains("宝马")) 97 { 98 al1.Add(sk); 99 } 100 } 101 for (int i = 0; i < al1.Count - 1; i++) 102 { 103 for (int j = i + 1; j < al1.Count; j++) 104 { 105 if (((swe)al1[i]).jiage > ((swe)al1[j]).jiage) 106 { 107 object zhong = al1[i]; 108 al1[i] = al1[j]; 109 al1[j] = zhong; 110 } 111 } 112 } 113 Console.WriteLine("宝马中最便宜的车型{0}", ((swe)al1[0]).chexing); 114 Console.WriteLine("-----------------------------------------------------------------------------------------"); 115 116 //选个轴距最长的 117 for (int i = 0; i < 8; i++) 118 { 119 for (int j = i + 1; j < 9; j++) 120 { 121 if (((swe)al[i]).mm < ((swe)al[j]).mm) 122 { 123 object zhong = al[i]; 124 al[i] = al[j]; 125 al[j] = zhong; 126 } 127 } 128 } 129 Console.WriteLine("轴距最长的{0}", ((swe)al[0]).chexing); 130 Console.WriteLine("-----------------------------------------------------------------------------------------"); 131 132 //选个轴距最长的,必须是奔驰 133 ArrayList al2 = new ArrayList(); 134 for (int i = 0; i < 9; i++) 135 { 136 swe sk = new swe(); 137 sk = (swe)al[i]; 138 if (sk.chexing.Contains("奔驰")) 139 { 140 al2.Add(sk); 141 } 142 } 143 for (int i = 0; i < al1.Count - 1; i++) 144 { 145 for (int j = i + 1; j < al1.Count; j++) 146 { 147 if (((swe)al2[i]).mm < ((swe)al2[j]).mm) 148 { 149 object zhong = al2[i]; 150 al2[i] = al2[j]; 151 al2[j] = zhong; 152 } 153 } 154 } 155 Console.WriteLine("轴距最长的{0}", ((swe)al2[0]).chexing); 156 Console.WriteLine("-----------------------------------------------------------------------------------------"); 157 158 //想要一辆油耗最低的车 159 for (int i = 0; i < 8; i++) 160 { 161 for (int j = i + 1; j < 9; j++) 162 { 163 if (((swe)al[i]).l < ((swe)al[j]).l) 164 { 165 object zhong = al[i]; 166 al[i] = al[j]; 167 al[j] = zhong; 168 } 169 } 170 } 171 Console.WriteLine("油耗最低的车是{0}", ((swe)al[8]).chexing); 172 Console.WriteLine("-----------------------------------------------------------------------------------------"); 173 Console.WriteLine("油耗最高的车是{0}", ((swe)al[0]).chexing); 174 Console.WriteLine("-----------------------------------------------------------------------------------------"); 175 176 //想要一辆油耗最低的奥迪车 177 ArrayList al3 = new ArrayList(); 178 for (int i = 0; i < 9; i++) 179 { 180 swe sk = new swe(); 181 sk = (swe)al[i]; 182 if (sk.chexing.Contains("奥迪")) 183 { 184 al3.Add(sk); 185 } 186 } 187 for (int i = 0; i < al3.Count - 1; i++) 188 { 189 for (int j = i + 1; j < al3.Count; j++) 190 { 191 if (((swe)al3[i]).l > ((swe)al3[j]).l) 192 { 193 object zhong = al3[i]; 194 al3[i] = al3[j]; 195 al3[j] = zhong; 196 } 197 } 198 } 199 Console.WriteLine("油耗最低的奥迪车{0}", ((swe)al3[0]).chexing); 200 Console.WriteLine("-----------------------------------------------------------------------------------------"); 201 202 //我只有50万,看看能买什么车 203 for (int i = 0; i < 9; i++) 204 { 205 swe sk = (swe)al[i]; 206 if (sk.jiage < 50) 207 { 208 Console.WriteLine("小于50万的车有{0}价格{1}轴距{2}油耗{3}", sk.chexing, sk.jiage, sk.mm, sk.l); 209 Console.WriteLine("-----------------------------------------------------------------------------------------"); 210 } 211 } 212 //我只需要60万以上的车,列出来所有车型和所有信息 213 for (int i = 0; i < 9; i++) 214 { 215 swe sk = (swe)al[i]; 216 if (sk.jiage > 60) 217 { 218 Console.WriteLine("大于60万的车有{0}----价格{1}----轴距{2}----油耗{3}", sk.chexing, sk.jiage, sk.mm, sk.l); 219 Console.WriteLine("-----------------------------------------------------------------------------------------"); 220 } 221 } 222 //轴距必须超过3m 223 for (int i = 0; i < 9; i++) 224 { 225 swe sk = (swe)al[i]; 226 if (sk.mm > 3000) 227 { 228 Console.WriteLine("轴距超过三米的{0}----价格{1}----轴距{2}----油耗{3}", sk.chexing, sk.jiage, sk.mm, sk.l); 229 Console.WriteLine("-----------------------------------------------------------------------------------------"); 230 } 231 } 232 233 //油耗在8.5以下都行,列列表 234 for (int i = 0; i < 9; i++) 235 { 236 swe sk = (swe)al[i]; 237 if (sk.l < 8.5) 238 { 239 Console.WriteLine("耗在8.5以下的{0}----价格{1}----轴距{2}----油耗{3}", sk.chexing, sk.jiage, sk.mm, sk.l); 240 Console.WriteLine("-----------------------------------------------------------------------------------------"); 241 } 242 }*/