zoukankan      html  css  js  c++  java
  • out参数,ref参数,params参数

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Linq;
      4 using System.Text;
      5 using System.Threading.Tasks;
      6 
      7 namespace _07out参数
      8 {
      9     class Program
     10     {
     11         static void Main(string[] args)
     12         {
     13             //写一个方法 求一个数组中的最大值、最小值、总和、平均值
     14             int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     15             ////将要返回的4个值,放到一个数组中返回
     16 
     17             //int[] res = GetMaxMinSumAvg(numbers);
     18             //Console.WriteLine("最大值是{0},最小值是{1},总和是{2},平均值是{3}", res[0], res[1], res[2], res[3]);
     19             //Console.ReadKey();
     20             int max1;
     21             int min1;
     22             int sum1;
     23             int avg1;
     24             bool b;
     25             string s;
     26             double d;
     27             Test(numbers, out max1, out min1, out sum1, out avg1, out b, out s, out d);
     28 
     29             Console.WriteLine(max1);
     30             Console.WriteLine(min1);
     31             Console.WriteLine(sum1);
     32             Console.WriteLine(avg1);
     33             Console.WriteLine(b);
     34             Console.WriteLine(s);
     35             Console.WriteLine(d);
     36             Console.ReadKey();
     37 
     38         }
     39         /// <summary>
     40         /// 计算一个数组的最大值、最小值、总和、平均值
     41         /// </summary>
     42         /// <param name="nums"></param>
     43         /// <returns></returns>
     44         public static int[] GetMaxMinSumAvg(int[] nums)
     45         {
     46             int[] res = new int[4];
     47             //假设 res[0] 最大值  res[1]最小值  res[2]总和  res[3]平均值
     48             res[0] = nums[0];//max
     49             res[1] = nums[0];//min
     50             res[2] = 0;//sum
     51             string name = "孙全";
     52             bool b = true;
     53             for (int i = 0; i < nums.Length; i++)
     54             {
     55                 //如果当前循环到的元素比我假定的最大值还大 
     56                 if (nums[i] > res[0])
     57                 {
     58                     //将当前循环到的元素赋值给我的最大值
     59                     res[0] = nums[i];
     60                 }
     61                 if (nums[i] < res[1])
     62                 {
     63                     res[1] = nums[i];
     64                 }
     65                 res[2] += nums[i];
     66             }
     67             //平均值
     68             res[3] = res[2] / nums.Length;
     69             return res;
     70 
     71 
     72         }
     73 
     74 
     75         /// <summary>
     76         /// 计算一个整数数组的最大值、最小值、平均值、总和
     77         /// </summary>
     78         /// <param name="nums">要求值得数组</param>
     79         /// <param name="max">多余返回的最大值</param>
     80         /// <param name="min">多余返回的最小值</param>
     81         /// <param name="sum">多余返回的总和</param>
     82         /// <param name="avg">多余返回的平均值</param>
     83         public static void Test(int[] nums, out int max, out int min, out int sum, out int avg, out bool b, out string s, out double d)
     84         {
     85             //out参数要求在方法的内部必须为其赋值
     86             max = nums[0];
     87             min = nums[0];
     88             sum = 0;
     89             for (int i = 0; i < nums.Length; i++)
     90             {
     91                 if (nums[i] > max)
     92                 {
     93                     max = nums[i];
     94                 }
     95                 if (nums[i] < min)
     96                 {
     97                     min = nums[i];
     98                 }
     99                 sum += nums[i];
    100             }
    101             avg = sum / nums.Length;
    102 
    103 
    104             b = true;
    105             s = "123";
    106             d = 3.13;
    107         }
    108 
    109 
    110 
    111     }
    112 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _10ref参数
     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 
    19         public static void JiangJin(ref double s)
    20         {
    21             s += 500;
    22 
    23         }
    24 
    25         public static void FaKuan(double s)
    26         {
    27             s -= 500;
    28         }
    29     }
    30 }
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _12params可变参数
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             //   int[] s = { 99, 88, 77 };
    14             //Test("张三",99,100,100,100);
    15             //Console.ReadKey();
    16 
    17             //求任意长度数组的和 整数类型的
    18             int[] nums = { 1, 2, 3, 4, 5 };
    19             int sum = GetSum(8,9);
    20             Console.WriteLine(sum);
    21             Console.ReadKey();
    22         }
    23 
    24         public static int GetSum(params int[] n)
    25         {
    26             int sum = 0;
    27             for (int i = 0; i < n.Length; i++)
    28             {
    29                 sum += n[i];
    30             }
    31             return sum;
    32         }
    33 
    34 
    35         public static void Test(string name, int id, params int[] score)
    36         {
    37             int sum = 0;
    38 
    39             for (int i = 0; i < score.Length; i++)
    40             {
    41                 sum += score[i];
    42             }
    43             Console.WriteLine("{0}这次考试的总成绩是{1},学号是{2}", name, sum, id);
    44         }
    45     }
    46 }
  • 相关阅读:
    MyEclipse 快捷键(转载)
    Microsoft .NET Framework 2.0安装失败
    数据库管理方面的电子书下载地址汇总
    增强MyEclipse的代码自动提示功能 (转载)
    有关Eclipse的自动完成&代码整理(摘)
    使用NewtonSoft.JSON.dll来序列化和发序列化对象(转载)
    hMailServer配置默认域名
    arcgis中的 style和serverstyle(转载)
    python function learning
    整理的设计模式博文列表
  • 原文地址:https://www.cnblogs.com/liuslayer/p/4451149.html
Copyright © 2011-2022 走看看