传值:将变量名中存放的值进行传输
private void 传值(int a) { a += 10; } static void Main(string[] args) { //实例化 初始化 这个类 Program hs = new Program(); int a = 0; hs.传值(a);//a不变 }
以上使用out传址 打印出来的值是15
但如果需要函数返回多个值时,可以用减号拼接起来,返回后用split分割开
public string Fanhui(string name , string sex,int age) { age += 10; return name + "-"+sex +"-"+ age; }
string s = hanshu.Fanhui("张三", "男", 33); string[] aa = s.Split('-'); foreach (string q in aa) { Console.WriteLine(q); }
结构体:自定义类型 值类型
一组变量的组合
需要定义的位置 class里面 main函数外面
里面包含的变量可以是多种数据类型的
例:学生信息的结构体:学号 姓名 性别 分数:
struct Student { public int xuehao; public string name; public string sex; public Score score; }
ArrayList al = new ArrayList(); Console.Write("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); for (int i = 0; i < a; i++) { Student sst = new Student(); Console.Write("请输入第{0}个学生的学号:", (i + 1)); sst.xuehao = int.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的姓名:", (i + 1)); sst.name = Console.ReadLine(); Console.Write("请输入第{0}个学生的性别:", (i + 1)); sst.sex = Console.ReadLine(); Console.Write("请输入第{0}个学生的分数:", (i + 1)); sst.score = double.Parse(Console.ReadLine()); al.Add(sst); } Console.WriteLine("所有人员信息输入完毕!请按回车键开始打印!"); Console.ReadLine(); for (int i = 0; i < al.Count; i++) { Student sst = (Student)al[i]; Console.WriteLine("第{0}个学生的学号是:{1},姓名是{2},性别是{3},分数是{4}。", (i + 1), sst.xuehao, sst.name, sst.sex, sst.score); }
存储下经典题目:
练习:超市购物问题
去超市选择要购买的商品
0.开始购买
1.洗发水 15元
2.牙刷 5元
3.可口可乐 3元
4.水杯 12元
5.毛巾 6元
6.结算
输入想要的物品编号,输入每种商品需要买几件
结算:需要列出来商品名称、单价、数量、
总价以及所有物品总价,结账时间
然后输入交给收银员的钱数,
不够,请继续交钱
够,结算,找钱
Console.WriteLine("0.开始购买"); //Console.WriteLine("1.洗发水 15元"); //Console.WriteLine("2.牙刷 5元"); //Console.WriteLine("3.可口可乐 3元"); //Console.WriteLine("4.水杯 12元"); //Console.WriteLine("5.毛巾 6元"); Console.WriteLine("6.结算(退出)"); Console.Write("请输入号码:"); for (int i = 0; i == 0; ) { int aa = int.Parse(Console.ReadLine()); if (aa == 0) { ArrayList al = new ArrayList(); int biao1 = 0; for (int j = 0; j == 0; ) { bool biaocuo = true; //string[] array = new string[3]; Shop array = new Shop(); Console.Clear(); if (al.Count > 0) { for (int k = 0; k < al.Count; k++) { //string[] yigou = (string[])al[k]; Shop yigou = (Shop)al[k]; //Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。", yigou[0], yigou[1], yigou[2]); Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。", yigou.name, yigou.price, yigou.shuliang); } } Console.WriteLine("1.洗发水 15元"); Console.WriteLine("2.牙刷 5元"); Console.WriteLine("3.可口可乐 3元"); Console.WriteLine("4.水杯 12元"); Console.WriteLine("5.毛巾 6元"); Console.WriteLine("6.结算"); Console.Write("请输入选项:"); int aaa = int.Parse(Console.ReadLine()); switch (aaa) { case 1: //array[0] = "洗发水"; //array[1] = "15"; array.name = "洗发水"; array.price = 15; biao1++; Console.Write("您选择的是洗发水,请问您需要多少瓶?"); break; case 2: //array[0] = "牙刷"; //array[1] = "5"; array.name = "牙刷"; array.price = 5; biao1++; Console.Write("您选择的是牙刷,请问您需要多少支?"); break; case 3: //array[0] = "可口可乐"; //array[1] = "3"; array.name = "可口可乐"; array.price = 3; biao1++; Console.Write("您选择的是可口可乐,请问您需要多少瓶?"); break; case 4: //array[0] = "水杯"; //array[1] = "12"; array.name = "水杯"; array.price = 12; biao1++; Console.Write("您选择的是水杯,请问您需要多少个?"); break; case 5: //array[0] = "毛巾"; //array[1] = "6"; array.name = "毛巾"; array.price = 6; biao1++; Console.Write("您选择的是毛巾,请问您需要多少块?"); break; case 6: if (biao1 == 0) { Console.Clear(); Console.WriteLine("您什么也没有购买,您已经走出了超市。。。"); j = 1; i = 1; } else { double zong = 0; for (int k = 0; k < al.Count; k++) { //string[] yigou = (string[])al[k]; Shop yigou = (Shop)al[k]; //Console.WriteLine("您选择了{0},单价为{1}元,数量为{2},单品总价:{3}。", yigou[0], yigou[1], yigou[2], (int.Parse(yigou[1]) * int.Parse(yigou[2]))); Console.WriteLine("您选择了{0},单价为{1}元,数量为{2},单品总价:{3}。", yigou.name, yigou.price, yigou.shuliang, yigou.price*yigou.shuliang); //zong += int.Parse(yigou[1]) * int.Parse(yigou[2]); zong += yigou.price * yigou.shuliang; } Console.Write("总价:" + zong + "元。请缴费:"); double erjiao = 0; for (int l = 0; l == 0; ) { double jiao = double.Parse(Console.ReadLine()); jiao += erjiao; if (jiao >= zong) { Console.WriteLine("交易成功,交易时间为:" + DateTime.Now); Console.WriteLine("找零:" + (jiao - zong) + "元。"); Console.WriteLine("谢谢惠顾!再见!"); l = 1; j = 1; i = 1; } else { erjiao = jiao; Console.Write("缴费金额不足,请继续缴费:"); } } } break; default: Console.WriteLine("查无此商品!请按回车键继续选择商品!"); Console.ReadLine(); biaocuo = false; break; } if (i == 0 && j == 0 && biaocuo == true) { //array[2] = Console.ReadLine(); array.shuliang = int.Parse(Console.ReadLine()); //Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。请按回车键继续购买!", array[0], array[1], array[2]); Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。请按回车键继续购买!", array.name, array.price, array.shuliang); al.Add(array); Console.ReadLine(); } } } else if (aa == 6) { Console.Clear(); Console.WriteLine("您什么也没有购买!您已走出超市。。。"); i = 1; } else { Console.Write("输入有误!请重新输入:"); } }