传值、传址 结构体
一、 传值:将变量名中存放的值进行传输
传址:将这个变量名直接传输过去,
若在另一边有赋值情况,这边的值会发生变化
传值
public void Hanshu(int a)
{
a += 10;
}
只是定义了个变量
public void hanshu1(int a , out int b,out int c)
{
b = a + 10;
c = b;
}
在主函数中,先实例化
int a = 5;
int rr;
int tt;
hanshu.hanshu1(a, out rr, out tt);
Console.WriteLine(rr);
Console.WriteLine(tt);
可得:rr=tt=15
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; ---这里声明了一个Score类型的score,Score 则是下面定义的另一个结构体类型
}
struct Score 这里定义了一个名为Score 的结构体
{
public double yufen;
public double shufen;
public double yingfen;
}
例题:
超市问题
struct Shop { public string name; public double danjia; public int shul; } Console.WriteLine("0.开始购买"); Console.WriteLine("6.结算(退出)"); Console.Write("请输入您所选选项的代码:"); for (int i = 0; i == 0; ) { int a = int.Parse(Console.ReadLine()); if (a == 0) { int biao1 = 0; ArrayList al = new ArrayList(); for (int j = 0; j == 0; ) { Shop sp = new Shop(); bool biaocuo = true; Console.Clear(); if (al.Count > 0) { for (int k = 0; k < al.Count; k++) { Shop yigou = (Shop)al[k]; Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。", yigou.name, yigou.danjia, yigou.shul); } } 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 aa = int.Parse(Console.ReadLine()); switch (aa) { case 1: sp.name = "洗发水"; sp.danjia = 15; biao1++; Console.Write("请输入您所选商品的数量:"); break; case 2: sp.name = "牙刷"; sp.danjia = 5; biao1++; Console.Write("请输入您所选商品的数量:"); break; case 3: sp.name = "可口可乐"; sp.danjia = 3; biao1++; Console.Write("请输入您所选商品的数量:"); break; case 4: sp.name = "水杯"; sp.danjia = 12; biao1++; Console.Write("请输入您所选商品的数量:"); break; case 5: sp.name = "毛巾"; sp.danjia = 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++) { Shop yigou = (Shop)al[k]; Console.WriteLine("您选择了{0},单价为{1}元,数量为{2},单品总价:{3}。", yigou.name, yigou.danjia, yigou.shul, yigou.danjia * yigou.shul); zong += yigou.danjia * yigou.shul; } Console.Write("总价:" + zong + "元。请缴费:"); int erjiao = 0; for (int l = 0; l == 0; ) { int jiao = int.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) { sp.shul = int.Parse(Console.ReadLine()); Console.WriteLine("您选择了{0},单价为{1}元,数量为{2}。请按回车键继续购买!", sp.name, sp.danjia, sp.shul); al.Add(sp); Console.ReadLine(); } } } else if (a == 6) { Console.Clear(); Console.WriteLine("您没有选择商品,直接退出本超市!"); i = 1; } else { Console.WriteLine("输入错误!"); } }
请输入班级人数,输入每个人的学号,姓名,和语文分数、数学分数和英语分数(要求使用结构体),求班级里两个语文分数是最高分的学生的所有信息;数学分数是最高分的两个学生的所有信息;英语平均分。
struct Student { public int xuehao; public string name; public Score score; } struct Score { public double yufen; public double shufen; public double yingfen; } ArrayList al = new ArrayList(); Console.Write("请输入班级人数:"); int a = int.Parse(Console.ReadLine()); for (int i = 0; i < a; i++) { Student st = new Student(); Console.Write("请输入第{0}个学生的学号:", (i + 1)); st.xuehao = int.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的姓名:", (i + 1)); st.name = Console.ReadLine(); Console.Write("请输入第{0}个学生的语文分数:", (i + 1)); st.score.yufen = double.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的数学分数:", (i + 1)); st.score.shufen = double.Parse(Console.ReadLine()); Console.Write("请输入第{0}个学生的英语分数:", (i + 1)); st.score.yingfen = double.Parse(Console.ReadLine()); al.Add(st); } for (int i = 0; i < al.Count - 1; i++) { for (int j = i + 1; j < al.Count; j++) { Student s1 = (Student)al[i]; Student s2 = (Student)al[j]; if (s1.score.yufen < s2.score.yufen) { Object b = al[i]; al[i] = al[j]; al[j] = b; } } } Student yu = (Student)al[0]; Student yu1 = (Student)al[1]; Console.Write("语文成绩最高的2个是:" + yu.xuehao + "\t" + yu.name + "\t" + yu.score.yufen + "\t" + yu.score.shufen + "\t" + yu.score.yingfen + "\n"); Console.Write(yu1.xuehao + "\t" + yu1.name + "\t" + yu1.score.yufen + "\t" + yu1.score.shufen + "\t" + yu1.score.yingfen + "\n"); for (int i = 0; i < al.Count - 1; i++) { for (int j = i + 1; j < al.Count; j++) { Student s1 = (Student)al[i]; Student s2 = (Student)al[j]; if (s1.score.shufen < s2.score.shufen) { Object b = al[i]; al[i] = al[j]; al[j] = b; } } } Student shu = (Student)al[0]; Student shu1 = (Student)al[1]; Console.Write("数学成绩最高的2个是:" + shu.xuehao + "\t" + shu.name + "\t" + shu.score.yufen + "\t" + shu.score.shufen + "\t" + shu.score.yingfen + "\n"); Console.Write(shu1.xuehao + "\t" + shu1.name + "\t" + shu1.score.yufen + "\t" + shu1.score.shufen + "\t" + shu1.score.yingfen + "\n"); double sum = 0; for (int i = 0; i < al.Count; i++) { Student s1 = (Student)al[i]; sum += s1.score.yingfen; } Console.WriteLine("英语平均分:" + sum / a); Console.ReadLine();
车辆信息
//所有在售车辆中最便宜的 //求宝马中最便宜的车型是什么,所有信息列出 //选个轴距最长的 //选个轴距最长的,必须是奔驰 //想要一辆油耗最低的车 //想要一辆油耗最低的奥迪车 //我只有50万,看看能买什么车 //我只需要60万以上的车,列出来所有车型和所有信息 //轴距必须超过3m,列列表 //油耗在8.5以下都行,列列表
struct che { public string xh; public double jg; public double zj; public double yh; } ArrayList al = new ArrayList(); che b1 = new che(); che b2 = new che(); che b3 = new che(); che a1 = new che(); che a2 = new che(); che a3 = new che(); che be1 = new che(); che be2 = new che(); che be3 = new che(); b1.xh = "宝马320Li"; b1.jg = 38; b1.zj = 2920; b1.yh = 6.9; b2.xh = "宝马520Li"; b2.jg = 43; b2.zj = 3108; b2.yh = 7.2; b3.xh = "宝马730Li"; b3.jg = 89; b3.zj = 3210; b3.yh = 6.3; a1.xh = "奥迪A4L35TFSI"; a1.jg = 31; a1.zj = 2869; a1.yh = 6.2; a2.xh = "奥迪A6L30TFSI"; a2.jg = 43; a2.zj = 3012; a2.yh = 7.6; a3.xh = "奥迪A8L45TFSI"; a3.jg = 89; a3.zj = 3122; a3.yh = 8.1; be1.xh = "奔驰C200L"; be1.jg = 35; be1.zj = 2920; be1.yh = 6.1; be2.xh = "奔驰E260L"; be2.jg = 48; be2.zj = 3014; be2.yh = 6.7; be3.xh = "奔驰S320L"; be3.jg = 93; be3.zj = 3165; be3.yh = 8; al.Add(b1); al.Add(b2); al.Add(b3); al.Add(b1); al.Add(a1); al.Add(a2); al.Add(a3); al.Add(be1); al.Add(be2); al.Add(be3); #region for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((che)al[i]).jg > ((che)al[j]).jg) { object b = al[i]; al[i] = al[j]; al[j] = b; } } } Console.Write("最便宜的车是:" + ((che)al[0]).xh + "\t" + ((che)al[0]).jg + "\t" + ((che)al[0]).zj + "\t" + ((che)al[0]).yh + "\n"); 下面的方法只能在上面排好顺序后才可以使用!!! for (int i = 0; i < 9; i++) { if (((che)al[i]).xh.Contains("宝马")) { che baocheap = (che)al[i]; Console.Write("最便宜的宝马车是:" + baocheap.xh + "\t" + baocheap.jg + "\t" + baocheap.zj + "\t" + baocheap.yh + "\n"); break; } } ArrayList bao = new ArrayList(); for (int i = 0; i < 9; i++) { if (((che)al[i]).xh.Contains("宝马")) { bao.Add(al[i]); } } for (int i = 0; i < bao.Count - 1; i++) { for (int j = i + 1; j < bao.Count; j++) { if (((che)bao[i]).jg > ((che)bao[j]).jg) { object b = bao[i]; bao[i] = bao[j]; bao[j] = b; } } } Console.Write("宝马里最便宜的车是:" + ((che)bao[0]).xh + "\t" + ((che)bao[0]).jg + "\t" + ((che)bao[0]).zj + "\t" + ((che)bao[0]).yh + "\n"); #endregion #region for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((che)al[i]).zj < ((che)al[j]).zj) { object b = al[i]; al[i] = al[j]; al[j] = b; } } } Console.Write("轴距最长的车是:" + ((che)al[0]).xh + "\t" + ((che)al[0]).jg + "\t" + ((che)al[0]).zj + "\t" + ((che)al[0]).yh + "\n"); ArrayList ben = new ArrayList(); for (int i = 0; i < 9; i++) { if (((che)al[i]).xh.Contains("宝马")) { ben.Add(al[i]); } } for (int i = 0; i < ben.Count - 1; i++) { for (int j = i + 1; j < ben.Count; j++) { if (((che)ben[i]).zj > ((che)ben[j]).zj) { object b = ben[i]; ben[i] = ben[j]; ben[j] = b; } } } Console.Write("奔驰里面轴距最长的车是:" + ((che)bao[0]).xh + "\t" + ((che)bao[0]).jg + "\t" + ((che)bao[0]).zj + "\t" + ((che)bao[0]).yh); #endregion #region for (int i = 0; i < al.Count; i++) { if (((che)al[i]).jg <= 50) { Console.Write(((che)al[i]).xh + "\t" + ((che)al[i]).jg + "\t" + ((che)al[i]).zj + "\t" + ((che)al[i]).yh + "\n"); } } Console.WriteLine(); for (int i = 0; i < al.Count; i++) { if (((che)al[i]).jg > 60) { Console.Write(((che)al[i]).xh + "\t" + ((che)al[i]).jg + "\t" + ((che)al[i]).zj + "\t" + ((che)al[i]).yh + "\n"); } } for (int i = 0; i < al.Count; i++) { if (((che)al[i]).zj > 3000) { Console.Write(((che)al[i]).xh + "\t" + ((che)al[i]).jg + "\t" + ((che)al[i]).zj + "\t" + ((che)al[i]).yh + "\n"); } } for (int i = 0; i < al.Count; i++) { if (((che)al[i]).yh <8.5) { Console.Write(((che)al[i]).xh + "\t" + ((che)al[i]).jg + "\t" + ((che)al[i]).zj + "\t" + ((che)al[i]).yh + "\n"); } } #endregion for (int i = 0; i < 8; i++) { for (int j = i + 1; j < 9; j++) { if (((che)al[i]).yh > ((che)al[j]).yh) { object b = al[i]; al[i] = al[j]; al[j] = b; } } } Console.Write("油耗最低的车是:" + ((che)al[0]).xh + "\t" + ((che)al[0]).jg + "\t" + ((che)al[0]).zj + "\t" + ((che)al[0]).yh + "\n"); ArrayList ao = new ArrayList(); for (int i = 0; i < 9; i++) { if (((che)al[i]).xh.Contains("奥迪")) { ao.Add(al[i]); } } for (int i = 0; i < ao.Count - 1; i++) { for (int j = i + 1; j < ao.Count; j++) { if (((che)ao[i]).jg > ((che)ao[j]).jg) { object b = ao[i]; ao[i] = ao[j]; ao[j] = b; } } } Console.Write("奥迪里面油耗最低的车是:" + ((che)ao[0]).xh + "\t" + ((che)ao[0]).jg + "\t" + ((che)ao[0]).zj + "\t" + ((che)ao[0]).yh + "\n"); Console.ReadLine();