标签:
1.传真 传址
namespace 传值_传址 { class Program { //格式1:无参无返 public void LeiJia() { Console.Write("请输入一个正整数:"); int a = int.Parse(Console.ReadLine()); int sum = 0; for (int i = 1; i <= a;i++ ) { sum += i; } Console.WriteLine(sum); } //格式2:有参无返 public void LeiJia(int zhengshu) { int sum = 0; for (int i = 1; i <= zhengshu; i++) { sum += i; } Console.WriteLine(sum); } //格式3:有参有返 public int LeiJia1(int zhengshu) { int sum = 0; for (int i = 1; i <= zhengshu; i++) { sum += i; } return sum; } //格式4:无参有返 public int Leijia2() { Console.Write("请输入一个正整数:"); int a = int.Parse(Console.ReadLine()); int sum = 0; for (int i = 1; i <= a; i++) { sum += i; } return sum; } //写个函数,传值进去,传姓名、性别、年龄进入 //将年龄+10岁 //反馈回来 public string Fanhui(string name , string sex,int age) { age += 10; return name + "-"+sex +"-"+ age; } //传值 public void Hanshu(int a) { a += 10; } //传址 public void hanshu1(int a , out int b,out int c) { //int c = a + b; b = a + 10; c = b; } public int AA = 5; static void Main(string[] args) { //传值:将变量名中存放的值进行传输 //传址:将这个变量名直接传输过去, //若在另一边有赋值情况,这边的值会发生变化 //调用函数之前需要先初始化该Class 类 Program hanshu = new Program(); //Console.Write("请输入一个正整数:"); //int a = int.Parse(Console.ReadLine()); //hanshu.Hanshu(a); //Console.WriteLine(a); hanshu.AA = 100; Console.WriteLine(hanshu.AA); int a = 5; int rr; int tt; hanshu.hanshu1(a,out rr,out tt); Console.WriteLine(rr); //调用格式1 //hanshu.LeiJia(); //调用格式2 //hanshu.LeiJia(15); //调用格式3 //int sum = hanshu.LeiJia1(12); //Console.WriteLine(sum); //调用格式4 //int sum = hanshu.Leijia2(); //Console.WriteLine(sum); //string s = hanshu.Fanhui("张三","男",33); //string[] aa = s.Split(‘-‘); //foreach(string q in aa) //{ // Console.WriteLine(q); //}
2.结构体
结构体:自定义类型 值类型
一组变量的组合
需要定义的位置 class里面 main函数外面
里面包含的变量可以是多种数据类型的
1 namespace 结构体 2 { 3 class Program 4 { 5 6 7 struct Student 8 { 9 public int xuehao; 10 public string name; 11 public string sex; 12 public Score score; 13 } 14 15 struct Score 16 { 17 public double yufen; 18 public double shufen; 19 public double yingfen; 20 } 21 22 struct Shop 23 { 24 public string name; 25 public double price; 26 public int shuliang; 27 } 28 29 static void Main(string[] args) 30 { 31 //实例化结构体 32 //Student st = new Student(); 33 //st.xuehao = 1001; 34 //st.name = "张三"; 35 //st.sex = "男"; 36 //st.score = 33; 37 38 //Student st1 = new Student(); 39 //st1.xuehao = 1002; 40 //st1.name = "李四"; 41 //st1.sex = "女"; 42 //st1.score = 44; 43 44 45 //ArrayList al = new ArrayList(); 46 //Console.Write("请输入班级人数:"); 47 //int a = int.Parse(Console.ReadLine()); 48 //for (int i = 0; i < a;i++ ) 49 //{ 50 // Student sst = new Student(); 51 // Console.Write("请输入第{0}个学生的学号:",(i+1)); 52 // sst.xuehao = int.Parse(Console.ReadLine()) ; 53 // Console.Write("请输入第{0}个学生的姓名:", (i + 1)); 54 // sst.name = Console.ReadLine(); 55 // Console.Write("请输入第{0}个学生的性别:", (i + 1)); 56 // sst.sex = Console.ReadLine(); 57 // Console.Write("请输入第{0}个学生的分数:", (i + 1)); 58 // sst.score = double.Parse(Console.ReadLine()); 59 // al.Add(sst); 60 //} 61 //Console.WriteLine("所有人员信息输入完毕!请按回车键开始打印!"); 62 //Console.ReadLine(); 63 64 //for (int i = 0; i < al.Count;i++ ) 65 //{ 66 // Student sst = (Student)al[i]; 67 // Console.WriteLine("第{0}个学生的学号是:{1},姓名是{2},性别是{3},分数是{4}。",(i+1),sst.xuehao,sst.name,sst.sex,sst.score); 68 //} 69 70 71 ////实例化 72 //Student st = new Student(); 73 //st.score.yufen = 77; 74 //st.score.shufen = 88; 75 //st.score.yingfen = 99;
3.超市购物 例题 方法二
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("输入有误!请重新输入:"); } }
4.用结构体做
namespace 结构体 { class Program { struct Student { public int xuehao; public string name; public string sex; public double score; } //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; //} //struct Shop //{ // public string name; // public double price; // public int shuliang; //} static void Main(string[] args) { 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); }
标签:
原文地址:http://www.cnblogs.com/ordinaryk/p/5977651.html