10月19日--传值传址结构体
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了10月19日--传值传址结构体相关的知识,希望对你有一定的参考价值。
传值---传址
传值:将变量名中存放的值进行传输
private void 传值(int a) { a += 10; } static void Main(string[] args) { //实例化 初始化 这个类 Program hs = new Program(); int a = 0; hs.传值(a);//a不变 }
传址:将这个变量名直接传输过去,若在另一边有赋值情况,这边的值会发生变化
private void 传址(int a, out int b) { b = a + 10; } static void Main(string[] args) { Program hs = new Program(); int a = 0; int b = 0; hs.传址(a, out b);//b变 }
结构体
结构体:自定义类型 值类型一组变量的组合
定义位置 Class里面 main函数外面
结构体里面可以包含各种类型的数据
struct Student { public int xuehao; public string name; public string sex; public Score score; } struct Score { public double yufen; public double shufen; public double yingfen; } static void Main(string[] args) { //实例化结构体 Student st = new Student(); st.xuehao = 1001; st.name = "张三"; st.sex = "男"; st.score = 33; }
练习:
超市购物
while (true) { Program hanshu = new Program(); ArrayList al = new ArrayList(); bool bo = true; Console.Write("请输入0开始购买:"); try { int bh = int.Parse(Console.ReadLine()); if (bh == 0 && bo) { Console.WriteLine("1.洗发水 15元\\n2.牙刷 5元\\n3.可口可乐 3元\\n4.水杯 12元\\n5.毛巾 6元\\n6.结算"); bo = false; }else { Console.WriteLine("请输入0开始购买,输入回车从新选购"); } if (!bo) { while (true) { Console.Write("请输入1~5进行选购、6结账:"); int wpbh = int.Parse(Console.ReadLine()); if (wpbh >= 1 && wpbh < 6) { // gouwu st = new gouwu(); st.mingcheng = hanshu.biaonhaozhaoming(wpbh); st.bianhao = wpbh; Console.Write("请输入要购买" + st.mingcheng + "的数量:"); int wpsl = int.Parse(Console.ReadLine()); switch (wpbh) { case 1: st.danjia = 15; break; case 2: st.danjia = 5; break; case 3: st.danjia = 3; break; case 4: st.danjia = 12; break; case 5: st.danjia = 6; break; } st.shuliang = wpsl; al.Add(st); Console.Clear(); if (al.Count > 0) { int zongjia = 0; Console.WriteLine("┌───────────────────┐"); Console.WriteLine("│物品名称\\t单价\\t数量\\t总价\\t│"); for (int i = 0; i < al.Count; i++) { gouwu stt = (gouwu)al[i]; Console.WriteLine("│" + stt.mingcheng + "\\t" + stt.danjia + "\\t" + stt.shuliang + "\\t" + (stt.danjia * stt.shuliang) + "\\t│"); zongjia += stt.danjia * stt.shuliang; } Console.WriteLine("│ │"); Console.WriteLine("│" + DateTime.Now.ToString() + "\\t应付总价:" + zongjia + "\\t│"); Console.WriteLine("└───────────────────┘"); } Console.WriteLine("1.洗发水 15元\\n2.牙刷 5元\\n3.可口可乐 3元\\n4.水杯 12元\\n5.毛巾 6元\\n6.结算"); } else if (wpbh == 6) { Console.Clear(); if (al.Count > 0) { int yifuzong = 0; int zongjia = 0; Console.WriteLine("┌───────────────────┐"); Console.WriteLine("│物品名称\\t单价\\t数量\\t总价\\t│"); for (int i = 0; i < al.Count; i++) { gouwu stt = (gouwu)al[i]; Console.WriteLine("│" + stt.mingcheng + "\\t" + stt.danjia + "\\t" + stt.shuliang + "\\t" + (stt.danjia * stt.shuliang) + "\\t│"); zongjia += stt.danjia * stt.shuliang; } Console.WriteLine("│ │"); Console.WriteLine("│" + DateTime.Now.ToString() + "\\t应付总价:" + zongjia + "\\t│"); Console.WriteLine("└───────────────────┘"); while (zongjia != 0) { Console.Write("请输入钱数进行结账:"); int fq = int.Parse(Console.ReadLine()); zongjia -= fq; yifuzong += fq; if (zongjia < 0) { Console.WriteLine("您付了" + yifuzong + "元,找您" + Math.Abs(zongjia) + "元"); zongjia = 0; } else if (zongjia > 0) { Console.WriteLine("您付了" + yifuzong + "元,还需要" + zongjia + "元"); } } Console.WriteLine("谢谢光临,欢迎下次再来!"); } else { Console.WriteLine("您未购买任何东西无法结账"); } break; } else { Console.WriteLine("请输入正确的编号"); } } } } catch (Exception ex) { Console.WriteLine(ex); } Console.ReadKey(); }
以上是关于10月19日--传值传址结构体的主要内容,如果未能解决你的问题,请参考以下文章