在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
1.函数 1.函数 /// <summary> /// main /// 主函数(主方法)控制台的输出输入控制函数 /// </summary> /// <param name="args">字符串参数</param> static void Main(string[] args) //static 静态修饰符,void(int,string char)是无返回值+方法名(参数){ 方法体} { int num1 = int.Parse(Console.ReadLine()); int num2 = int.Parse(Console.ReadLine()); string num3= GetMax(num1, num2); Console.WriteLine(num3); //Run(); //ChangeSelf(); //Run(); //ChangeSelf(); //int num1 = 10; //Console.WriteLine(num1); //return ; //结束当前方法 //int num2 = 20; //Console.WriteLine(num2); } public static string GetMax(int num1,int num2) { int num3 = num1 > num2 ? num1 : num2; //Console.WriteLine(num3); return num3+""; } public static void Run() { Console.WriteLine("超级玛丽,跑啊跑,跳啊跳"); Console.WriteLine("超级玛丽,跑啊跑,跳啊跳"); Console.WriteLine("超级玛丽,跑啊跑,跳啊跳"); Console.WriteLine("超级玛丽,跑啊跑,跳啊跳"); Console.WriteLine("超级玛丽,跑啊跑,跳啊跳"); } public static void ChangeSelf() { Console.WriteLine("遇见一个砖块,撞一下无敌"); Console.WriteLine("屏幕闪烁,身体变大"); Console.WriteLine("遇到小怪,无敌消失"); }
例子1: public static int a = 3; static void Main(string[] args)//void 无返回值 { // int a = 3; Text(); Console.WriteLine(a);//作用域不同产生的结果不同 } public static void Text() { a = a + 5; } 例子2: Console.WriteLine("请输入年份"); string s = Console.ReadLine(); year= IsInt(s); bool b= IsRun(year); Console.WriteLine("{0}是闰年吗?{1}", year, b); } public static int IsInt(string s) { int year; while (true) { try { year = int.Parse(s); return year; } catch { Console.WriteLine("您输入的有误,请在输入一次"); s = Console.ReadLine(); } } } public static void Text() { a = a + 5; } public static bool IsRun(int year) { bool b = year % 400 == 0 || year % 4 == 0 && year % 100 != 0; return b; } 例子3: //利用方法求两个数的和与积 int num1 = 20; int num2 = 30; int[] s= GetSum(52,63);//实参 Console.WriteLine("和为{0},积为{1}",s[0],s[1]); } public static int[] GetSum(int a, int b)//形参,变量名字与main的变量名字没有任何关系 { //Console.WriteLine("和为" + (num1 + num2)); //Console.WriteLine("积为" + (num1 * num2)); int sum = a + b; int[] nums = { sum, (a * b) }; return nums; } 例子4: int[] nums = {10,54,63,55,45,65,23,54,33,65,46 }; int[] n= GetResult(nums); foreach (var item in n) { Console.WriteLine(item); } } public static int[] GetResult(int[] nums) { int sum = 0; int avg = 0; int min = nums[1]; int max=nums[5]; for (int i = 0; i < nums.Length; i++) { if (nums[i]>max) { max = nums[i]; } if (nums[i]<min) { min = nums[i]; } sum += nums[i]; } avg = sum / nums.Length; int[] n = { max,min,sum,avg}; return n; }
using System; namespace _002_函数 { class Program { static void Main(string[] args) { int year; Console.WriteLine("请输入年份"); string s = Console.ReadLine(); year = IsInt(s); bool b = IsRun(year); Console.WriteLine("{0}是闰年吗?{1}",year,b); } public static int IsInt(string s) { int year; while (true) { try { year = int.Parse(s); return year; } catch { Console.WriteLine("输入有误,请重新输入"); s = Console.ReadLine(); } } } public static bool IsRun(int year) { bool b = year % 400 == 0 || year % 4 == 0 && year % 100 != 0; return b; } } }
using System; namespace _006_作业3 { class Program { static void Main(string[] args) { Console.WriteLine("请输入用户名和密码:"); string name = Console.ReadLine(); string mima = Console.ReadLine(); int password = IsInt(mima); Name(name,password); } #region 判断密码 public static int IsInt(string mima) { while (true) { int password = 0; try { password = int.Parse(mima); return password; } catch { Console.WriteLine("密码输入有误,请重新输入"); mima = Console.ReadLine(); } } } #endregion #region 判断用户名和密码 public static void Name(string name,int password) { int i = 1; while (i < 5) { if (name == "admin" && password == 123456) { Console.WriteLine("登录成功"); break; } else { Console.WriteLine("密码或用户名错误,请重新输入"); name = Console.ReadLine(); string mima = Console.ReadLine(); password = IsInt(mima); } i++; } } #endregion } }
using System; namespace _007_作业4_菲波那切数列 { class Program { static void Main(string[] args) { int a = 0; int b = 1; int index = SS(a, b); Console.WriteLine("斐波那数列第十个数为"+index); } public static int SS(int a,int b) { int c = 0; for (int i = 1; i < 9; i++) { c = a + b; a = b; b = c; } return c; } } }
using System; namespace _008_作业5_比较三个数大小 { class Program { static void Main(string[] args) { Console.WriteLine("请输入三个数"); int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); int c = int.Parse(Console.ReadLine()); IsInt(a, b, c); } public static void IsInt(int a,int b,int c) { int max = a > b ? a : b; max = max > c ? max : c; int min = a < b ? a : b; min = min < c ? min : c; int zj = (a + b + c) - max - min; Console.WriteLine("最大值为"+max+"中间值为"+zj+"最小值为"+min); } } }
using System; namespace _009_作业6_计算器 { class Program { static void Main(string[] args) { Console.WriteLine("请输入要进行的计算,和数字"); string c = Console.ReadLine(); PanDuanFuHao(c); } #region 判断运算符 public static void PanDuanFuHao(string c) { int a = int.Parse(Console.ReadLine()); int b = int.Parse(Console.ReadLine()); switch (c) { case "+": JF( a,b); break; case "-": JianF(a, b); break; case "*": CF(a, b); break; case "/": ChuF(a, b); break; default: Console.WriteLine("请重新输入"); break; } } #endregion #region 加 public static void JF(int a,int b) { Console.WriteLine("和为"+(a+b)); } #endregion #region 减 public static void JianF(int a, int b) { Console.WriteLine("差为" + (a - b)); } #endregion #region 乘 public static void CF(int a, int b) { Console.WriteLine("积为" + (a * b)); } #endregion #region 除 public static void ChuF(int a, int b) { Console.WriteLine("商为" + (a / b)); } #endregion } }
2. 函数的重载 static void Main(string[] args) { Sum(10,20); //方法构成重载的条件:1.方法名必须一样,参数列表不同(参数类型不同,参数个数不同)2.与返回值无关 Console.WriteLine(); } public static void Sum(int num1, int num2) { Console.WriteLine(num1 + num2); } public static int Sum(int num3, int num4,int num5) { return num3 + num4 ; } public static string Sum(int a, bool b) { return ""; } public static void Sum(int num6, string num7) { Console.WriteLine(num6+int.Parse(num7)); }
using System; namespace _014_函数的重载 { class Program { public static int MaxValue(params int[] arry) { int maxvalue = arry[0]; for (int i = 1; i < arry.Length; i++) { if (arry[i] > maxvalue) { maxvalue = arry[i]; } } return maxvalue; } public static double MaxValue(params double[] arry) { double maxvalue = arry[0]; for (int i = 1; i < arry.Length; i++) { if (arry[i] > maxvalue) { maxvalue = arry[i]; } } return maxvalue; } static void Main(string[] args) { int res = MaxValue(123, 25, 63); double res1 = MaxValue(3.14, 2.28, 56.3); Console.WriteLine(res1); } } }
例子:ref static void Main(string[] args) { //ref int num = 20; int num1 = 30; int sum=0; Sum(ref sum, num, num1);//ref:把实参中的值带入形参参与运算,运算完毕赋值再带出来(ref的值必须赋予初值) Sum(ref num,ref num1); Console.WriteLine(num+" "+num1); } public static void Sum(ref int sum, int num, int num1) { sum = num + num1; } public static void Sum(ref int a,ref int b) { int sum = a + b; a = a > b ? a : b; b = sum - a; }
using System; namespace _015_ref { class Program { static void Main(string[] args) { int num1 = 10; int num2 = 50; MaxValue(ref num1,ref num2);//把实参的值带入形参参与运算,运算wanbi 赋值在带出来(ref值必须赋予初值) Console.WriteLine(num1+" "+num2); } public static void MaxValue( ref int num1, ref int num2) { int sum = num1+num2; num1 = num1 > num2 ? num1 : num2; num2 = sum - num1; } } }
例子:out Console.WriteLine("请输入数字"); string num = Console.ReadLine(); int number; bool b= TryPase(num,out number); if (b) { Console.WriteLine(number); } else { Console.WriteLine(num); } } public static bool TryPase(string num,out int number) { number = 0; try { number = int.Parse(num); return true; } catch { return false; } }
using System; namespace _016_out { class Program { static void Main(string[] args) { int a = 12; int b = 2; Sum(out a, out b); Console.WriteLine(a + " " + b); } public static void Sum(out int a, out int b) { a = 100; b = 12; a = a + b; } } }
using System; namespace _018_out求和_平均值_ { class Program { static void Main(string[] args) { int[] num = { 12, 25, 33, 42, 36 }; Max(num, out int sum, out double average, out int max, out int min); Console.WriteLine(sum+" "+average+" "+max+" "+min); } public static void Max(int[]num1,out int sum,out double average,out int max,out int min) { sum = 0; max = int.MinValue; min = int.MaxValue; average = 0; for (int i = 0; i < num1.Length; i++) { if (num1[i] > max) { max = num1[i]; } if (num1[i] < min) { min = num1[i]; } sum += num1[i]; } average = sum / num1.Length; } } }
3.函数的递归 using System; namespace _010_函数的递归调用 { class Program { public static int F(int n) { if (n == 0) return 2; if (n == 1) return 3; return F(n - 1) + F(n - 2); } static void Main(string[] args) { //f(n)=f(n-1)+f(n-2) f(0)=2 f(1)=3 ,用程序求得f(40) int res = F(40); Console.WriteLine(res); } } } public static int i = 0; static void Main(string[] args) { //递归:自己的方法调用自己的方法就是递归 //int num=0; //SayStory(); //用递归求100以内正数的和 int num = 100; int sum = GetSum(100);// getSumDG(99)+100//getSumDG(98)+99+100 sum = GetSum(99) + 100; sum = (GetSumDG(98) + 99) + 100; sum = (GetSumDG(97) + 98) + 99 + 100; } public static int GetSumDG(int i) { if (i == 1) { return 1; } return GetSumDG(i - 1) + i; // getSumDG(99)+100 //getSumDG(98)+99+100 全部评论
专题导读
热门推荐
热门话题
阅读排行榜
|
请发表评论