函数
void是没有返回值,括号内是参数
private只能在当前类里面用,public公用的,可以在整个命名空间使用
函数 函数创建与使用 示例一 身份证截取 static public string jiequ(string a) {
string p = a.Substring(6, 8); Console.WriteLine(p); string n = a.Substring(6, 4); string y = a.Substring(10, 2); string d = a.Substring(12, 2); return "您的生日是:" + n + "年" + y + "月" + d + "日";
} static void Main(string[] args) { Console.WriteLine("输入身份证号:"); Console.WriteLine(jiequ(Console.ReadLine())); }
一群羊赶到村庄去卖,美国一个村庄对视之前总数的1/2+1只,过了7个村庄之后还剩两只,问最初赶了几只羊?? 递归法 public int dgqiuyang(int cun) { int sum = 0; if (cun==7) { return 2; } sum=2*(dgqiuyang(cun+1)+1); return sum; } static void Main(string[] args) { hanshu hb = new hanshu(); double x = hb.dgqiuyang(1); Console.Write("求羊"+x); Console.ReadLine (); }
打印图形 菱形 static public void lingxing(int x) {
string t = "※"; for (int i = 0; i < x - 1; i++)//菱形上部 {
for (int k = x; k > i + 1; k--) {
Console.Write(" ");
} for (int k = 2 * x; k > 2 * (x - i) - 1; k--) {
Console.Write(t); }
Console.WriteLine(); }
for (int i = 0; i < x; i++)//菱形下部 { for (int k = 0; k < i; k++) {
Console.Write(" ");
} for (int k = 0; k < 2 * (x - i) - 1; k++) {
Console.Write(t); } Console.WriteLine(); }
}
方形 static public void fangxing(int x,int y) { string t = "※"; for (int i = 0; i < y; i++) {
for (int k = 0; k <x; k++) { Console.Write(t); } Console.WriteLine(); } } 平行四边形 static public void pingxing(int x,int y) { string t = "※"; for (int i = 0; i < y ; i++) {
for (int k = x-i; k<=x; k++) {
Console.Write(" ");
} for (int k =0; k <x; k++) {
Console.Write(t); }
Console.WriteLine(); } } static void Main(string[] args) { while (true) { try {
Console.WriteLine("请选择:"); Console.WriteLine("1:菱形 \t2:方形 \t3:平行四边形\t4:尖三角\t5:杨辉三角\t6:瘦菱形"); int n = Convert.ToInt32(Console.ReadLine()); switch (n) { case 1: Console.Write("需要打印行数:"); int m = Convert.ToInt32(Console.ReadLine()); lingxing(m); break; case 2: Console.Write("需要打印长度:"); int m1 = Convert.ToInt32(Console.ReadLine()); Console.Write("需要打印宽度:"); int m2 = Convert.ToInt32(Console.ReadLine()); fangxing(m1,m2); break; case 3: Console.Write("需要打印长度:"); int m3 = Convert.ToInt32(Console.ReadLine()); Console.Write("需要打印行数:"); int m4 = Convert.ToInt32(Console.ReadLine()); pingxing(m3,m4); break; default: break; }
} catch (Exception) { } } }