zoukankan      html  css  js  c++  java
  • 学习笔记_第十天_方法_方法的三个高级参数

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {   
    11         //写一个方法 求一个数组中的最大值、最小值、总和、平均值
    12         //将返回的4个值,放在一个数组中返回
    13         static void Main(string[] args)
    14         {
    15             int[] numbs = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    16             int[] reslut = GetMaxMinSumAvg(numbs);
    17             Console.WriteLine("输入数组的最大值是{0},最小值是{1},总和是{2},平均值是{3}",reslut[0], reslut[1], reslut[2], reslut[3]);
    18             Console.ReadKey();
    19         }
    20         /// <summary>
    21         /// 计算一个数组的最大值、最小值、总和、平均值
    22         /// </summary>
    23         /// <param name="numbers">要计算的数组</param>
    24         /// <returns>最大值、最小值、总和、平均值形成的数组</returns>
    25         public static int[] GetMaxMinSumAvg(int[] numbers)
    26         {
    27             // 假设res[0]是最大值,res[1]是最小值,res[0]是总和,res[0]是平均值
    28             int[] res = new int[4];
    29             res[0] = numbers[0];//Max
    30             res[1] = numbers[0];//Min
    31             res[2] = 0;
    32             for (int i = 0; i < numbers.Length; i++)
    33             {
    34                 //如果当前循环到的元素比我假定的最大值还大
    35                 if (res[0]<numbers[i])
    36                 {
    37                     //将循环到的元素赋值给res[0]
    38                     res[0] = numbers[i];
    39                 }
    40                 if (res[1] <numbers[i])
    41                 {
    42                     //将循环到的元素赋值给res[0]
    43                     res[1] = numbers[i];
    44                 }
    45                 res[2] += numbers[i];
    46             }
    47             res[3] = res[2] / numbers.Length;
    48             return res;
    49         }
    50     }
    51 }

     (1)out 参数

    如果在一个方法中,需要返回多个相同类型的值的时候,考虑使用数组。

    但是如果在一个方法中,需要返回多个不同类型的值的时候,就得考虑使用out参数。

    out参数:侧重于在一个方法中,可以返回多个不同类型的值。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {   
    11         //写一个方法 求一个数组中的最大值、最小值、总和、平均值
    12         //将返回的4个值,放在一个数组中返回
    13         static void Main(string[] args)
    14         {
    15             int min1 = 0;
    16             int max1 = 0;
    17             int sum1 = 0;
    18             int avg1= 0;
    19             int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    20             Test(numbers,out max1,out min1,out sum1,out avg1);
    21             Console.WriteLine(max1);
    22             Console.WriteLine(min1);
    23             Console.WriteLine(sum1);
    24             Console.WriteLine(avg1);
    25             Console.ReadKey();
    26         }
    27         /// <summary>
    28         /// 用out参数形式来计算一个数组的最大值、最小值、总和、平均值
    29         /// </summary>
    30         /// <param name="numbs">要求值的数组</param>
    31         /// <param name="max">多余返回的最大值</param>
    32         /// <param name="min">多余返回的最小值</param>
    33         /// <param name="sum">多余返回的总和</param>
    34         /// <param name="avg">多余返回的平均值</param>
    35         public static void Test(int[] numbs,out int max,out int min,out int sum,out int avg)
    36         {
    37             //out 参数要求在方法的内部必须为其赋值
    38             max = 0;
    39             min = 0;
    40             sum = 0;
    41             for (int i = 0; i < numbs.Length; i++)
    42             {
    43                 if (numbs[i]>max)
    44                 {
    45                     max = numbs[i];
    46                 }
    47                 if (numbs[i]<min)
    48                 {
    49                     min = numbs[i];
    50                 }
    51                 sum += numbs[i];
    52             }
    53             avg = sum / numbs.Length;
    54         }
    55     }
    56 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         //分别提示用户输入用户名和密码
    12         //写一个方法来判断用户输入的是否正确
    13         //换回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息
    14         //如果用户名错误,除了返回登陆结果以外,还要返回一个“用户名错误”
    15         //密码错误
    16         static void Main(string[] args)
    17         {
    18             Console.WriteLine("请输入用户名:");
    19             string _name = Console.ReadLine();
    20             Console.WriteLine("请输入密码:");
    21             string _pwd = Console.ReadLine();
    22             string _msg;
    23             bool b=IsBool(_name,_pwd,out _msg);
    24             Console.WriteLine("登陆结果:{0}",b);
    25             Console.WriteLine("登陆信息:{0}",_msg);
    26             Console.ReadKey();
    27         }
    28         /// <summary>
    29         /// 判断登陆是否成功
    30         /// </summary>
    31         /// <param name="name">用户名</param>
    32         /// <param name="pwd">密码</param>
    33         /// <param name="msg">多余返回的登陆信息</param>
    34         /// <returns>返回登陆结果</returns>
    35         public static bool IsBool(string name, string pwd, out string msg)
    36         {
    37             if (name == "admin" && pwd == "888888")
    38             {
    39                 msg = "登陆成功";
    40                 return true;
    41             }
    42             else if (name == "admin")
    43             {
    44                 msg = "密码错误";
    45                 return false;
    46             }
    47             else if (pwd == "888888")
    48             {
    49                 msg = "您输入的用户名不存在";
    50                 return false;
    51             }
    52             else
    53             {
    54                 msg = "用户名/密码错误";
    55                 return false;
    56             }
    57         }
    58     }
    59 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         //分别提示用户输入用户名和密码
    12         //写一个方法来判断用户输入的是否正确
    13         //换回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息
    14         //如果用户名错误,除了返回登陆结果以外,还要返回一个“用户名错误”
    15         //密码错误
    16         static void Main(string[] args)
    17         {
    18             int num;
    19             bool b = int.TryParse("123",out num);
    20             Console.WriteLine(b);
    21             Console.WriteLine(num);
    22             Console.ReadKey();
    23         }
    24         public static bool MyTryParse(string s,out int result)
    25         {
    26 
    27             result = 0;
    28             try
    29             {
    30                 result = Convert.ToInt32(s);
    31                 return true;
    32             }
    33             catch
    34             {
    35                 return false;
    36             }
    37         }
    38     }
    39 }

     (2)ref参数

    能够将一个变量带入一个方法中进行改变,改变完成后,再将改变后的值带出方法。ref参数要求在方法外部必须为其赋值,而方法内部可以不赋值。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             double salary = 5000;
    14             double _mysalary= JiangJin(salary);
    15             Console.WriteLine(_mysalary);
    16             Console.ReadKey();
    17         }
    18         public static double JiangJin(double numbs)
    19         {
    20             numbs += 500;
    21             return numbs;
    22         }
    23     }
    24 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             double salary = 5000;
    14             JiangJin(ref salary);
    15             Console.WriteLine(salary);
    16             Console.ReadKey();
    17         }
    18         public static void JiangJin(ref double numbs)
    19         {
    20             numbs += 500;   
    21         }
    22     }
    23 }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace 方法002
    {
        class Program
        {
            //两种交换变量的方法
            static void Main(string[] args)
            {
                int _n1 = 10;
                int _n2 = 20;
                //int temp = n1;
                //n1 = n2;
                //n2 = temp;
                //Console.WriteLine(n1);
                //Console.WriteLine(n2);
                //Console.ReadKey();
    
                //int n1 = 10;
                //int n2 = 20;
                //n1 = n1 - n2;
                //n2 = n1 + n2;
                //n1 = n2 - n1;
                Test(ref _n1,ref _n2);
                Console.WriteLine(_n1);
                Console.WriteLine(_n2);
                Console.ReadKey();
            }
            public static void Test(ref int n1,ref int n2)
            {
                int temp = n1;
                n1 = n2;
                n2 = temp;
            }
        }
    }

     (3)params参数

    当实参列表中跟可变参数数组类型一致的元素都当作数组的元素去处理。

    注:参数数组必须是形参列表中的最后一个参数。   即:parmas可变参数必须是形参列表中的最后一个参数。

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         
    12         static void Main(string[] args)
    13         {
    14             
    15             Test("张三",99,88,20);
    16             Console.ReadKey();
    17         }
    18         public static void Test(string name,params int[] score)
    19         {
    20             int sum = 0;
    21             for (int i = 0; i < score.Length; i++)
    22             {
    23                 sum += score[i];
    24             }
    25             Console.WriteLine("{0}这次的考试总成绩是{1}",name,sum);
    26         }
    27     }
    28 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace 方法002
     8 {
     9     class Program
    10     {
    11         //求任意长度数组的和(整数类型的)
    12         static void Main(string[] args)
    13         {
    14             //int[] numbers = { 0, 1, 2, 3, 45, 5, 6 };
    15             int sum = GetSum(6,5,5,41,5,515);
    16             Console.WriteLine(sum);
    17             Console.ReadKey();
    18         }
    19         public static int GetSum(params int[] num)
    20         {
    21             int sum = 0;
    22             for (int i = 0; i < num.Length; i++)
    23             {
    24                 sum += num[i];
    25             }
    26             return sum;
    27         }
    28     }
    29 }
  • 相关阅读:
    2021NUAA暑假集训 Day3 题解
    2021NUAA暑假集训 Day2 题解
    2021NUAA暑期模拟赛部分题解
    CodeForces 1038D Slime
    UVA 11149 Power of Matrix
    UVA 10655 Contemplation! Algebra
    UVA 10689 Yet another Number Sequence
    HDU 4549 M斐波那契数列
    HDU 4990 Reading comprehension
    CodeForces 450B Jzzhu and Sequences
  • 原文地址:https://www.cnblogs.com/NBOWeb/p/7458000.html
Copyright © 2011-2022 走看看