一、函数
1.我们在Main()函数中,调用Test()函数,我们管vain()函数称之为调用者,管Test()函教称之为被调用者。
如果被调用者想要得到调用者的值:
1)、传递参数。
2)、使用静态字段来模拟全局变量(C#中没有全局变量)
如果调用者想要得到被调用者的值:
1)、返回值
class Program
{
//字段 属于类的字段
public static int _number = 9;
static void Main(string[] args)
{
int a = 4;
int b=Test(a);
Console.WriteLine(b);
Console.WriteLine(_number);
Console.ReadKey();
}
public static int Test(int n)
{
n += _number;
return n;
}
}
2.不管是实参还是形参,都是在内存中开辟了空间的。
3.方法的功能一定要单一.
static void Main(string[] args)
{
Console.WriteLine("请输入一个数");
String s = Console.ReadLine();
int i= GetNumber(s);
Console.WriteLine(i);
Console.ReadKey();
}
// 读取输入的整数,定义成方法,多次调用(如果用户输入的是数字, 则返回, 否则提示用户重新输入)
public static int GetNumber(String str)
{
while (true)
{
try
{
int i = Convert.ToInt32(str);
return i;
}
catch
{
Console.WriteLine("输入错误,请重新输入");
str = Console.ReadLine();
}
}
}
}
//写一个方法求一个数组中的最大值、最小值、总和、平均值
int[] numbers = { 1, 5, 3, 6, 9, 47, 55, 69 };
//将要返回的4个值,放到一个数组中返回
int[] res = GetMaxMinSumAVG(numbers);
Console.WriteLine("最大值{0},最小值{1},总和是{2},平均值是{3}",res[0],res[1],res[0],res[3]);
Console.ReadKey();
}
/// <summary>
/// 计算一个数组的最大值、最小值、平均值、和
/// </summary>
/// <param name="nums">数组</param>
/// <returns></returns>
public static int[] GetMaxMinSumAVG(int[] nums)
{
int[] res = new int[4];
//假设res[0]最大值 res[1]最小值 res[2]总和 res[3]平均值
res[0] = nums[0];//max
res[1] = nums[0];//min
res[2] = 0;
for (int i = 0; i < nums.Length; i++)
{
//如果当前循环到的元素比我假定的最大值还大
if (nums[i]>res[0])
{
// 将当前循环到的元素赋值给我的最大值
res[0] = nums[i];
}
if (nums[i] < res[1])
{
res[0] = nums[i];
}
res[2] += nums[i];
}
res[3] = res[2] / nums.Length;
return res;
}
二、重要参数 out、 ref、params
1.out参数。
如果你在一个方法中,返回多个相同类型的值的时候,可以考虑返回一个数组。但是,如果返回多个不同类型的值的时候,返回数组就不行了,那么这个时候,我们可以考虑使用out参数。
out参数就侧重于在一个方法中可以返回多个不同类型的值。
static void Main(string[] args)
{
int[] numbers = { 1, 5, 3, 6, 9, 47, 55, 69 };
int max;
int min;
int sum;
int avg;
Test(numbers,out max, out min, out avg, out sum);
Console.WriteLine("最大值{0},最小值{1},总和是{2},平均值是{3}",max,min,sum,avg);
Console.ReadKey();
}
/// <summary>
/// 计算一个数组的最大值、最小值、平均值、和
/// </summary>
/// <param name="nums">要求值得数组</param>
/// <param name="max">最大值</param>
/// <param name="min">最小值</param>
/// <param name="avg">平均值</param>
/// <param name="sum">和</param>
public static void Test(int[] nums,out int max,out int min,out int avg,out int sum)
{
// out参数要求在方法的内部必须为其赋值
max = nums[0];
min = nums[0];
sum = 0;
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;
}
//分别的提示用户输入用户名和密码
//你写一个方法来判断用户输入的是否正确
//返回给用户一个登陆结果,并且还要单独的返回给用户一个登陆信息/如果用户名错误,除了返回登陆结果之外,还要返回一个“用户名错误"
//“密码错误”
static void Main(string[] args)
{
Console.WriteLine("请输入用户名");
String username = Console.ReadLine();
Console.WriteLine("请输入密码");
String password = Console.ReadLine();
String msg;
bool b=IsLogin(username, password, out msg);
Console.WriteLine("登陆结果{0}",b);
Console.WriteLine("登陆信息{0}",msg);
Console.ReadKey();
}
/// <summary>
/// 判断登陆是否成功
/// </summary>
/// <param name="name">用户名</param>
/// <param name="pwd">密码</param>
/// <param name="msg">登陆信息</param>
/// <returns>返回登陆结果</returns>
public static bool IsLogin(String name,string pwd,out string msg)
{
if (name == "admin" && pwd == "888888")
{
msg = "登陆成功";
return true;
}else if (name == "admin")
{
msg = "密码错误";
return false;
}else if (pwd == "888888")
{
msg = "用户名错误";
return false;
}
else
{
msg ="未知错误";
return false;
}
}
2.ref参数
能够将一个变量带入一个方法中进行改变,改变完成后,再讲改变后的值带出方法。
ref参数要求在方法外必须为其赋值,而方法内可以不赋值。
static void Main(string[] args)
{
int n1 = 10;
int n2 = 20;
Exchang(ref n1, ref n2);
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.ReadKey();
}
public static void Exchang( ref int n1,ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
//n1=n1-n2;
//n2=n1+n2;
//n1=n2-n1;
}
3.params可变参数
将实参列表中跟可变参数数组类型一致的元素都当做数组的元素去处理
params可变参数必须是形参列表中的最后一个元素。
static void Main(string[] args)
{
// int[] num = { 95, 47, 02 };
Test("战神", 95, 47, 02,63,25);
Console.ReadKey();
}
public static void Test(String name, int id,params int[] score)
{
int sum = 0;
for (int i = 0; i < score.Length; i++)
{
sum += score[i];
}
Console.WriteLine("{0}的总成绩为{1},学号为{2}",name,sum,id);
}
方法的重载
概念: 方法的重载指的就是方法的名称相同给,但是参数不同。、
参数不同,分为两种情况
1)、如果参数的个数相同,那么参数的类型就不能相同。
2)、如果参数的类型相同,那么参数的个数就不能相同。
★★★方法的重教足返回值没有关系。
public static void T(int n1,int n2)
{
}
public static void T(double n1, double n2)
{
}
public static void T(int n1, int n2,int n3)
{
}
方法的递归
方法自己调用自己.
找出一个文件夹中所有的文件。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
// 提示用户输入两个数字计算这两个数字之间所有整数的和
// 1、用户只能输入数字
Console.WriteLine("请输入第一个数字");
String strNumberOne = Console.ReadLine();
int numberOne = GetNumber(strNumberOne);
Console.WriteLine("请输入第二个数字");
string strNumberTwo = Console.ReadLine();
int numberTwo = GetNumber(strNumberTwo);
//2、要求第一个数字必须比第二个数字小就重新输入
JudgeNumber(ref numberOne, ref numberTwo);
//3、计算两个数字之间和
int sum = GetSum(numberOne, numberTwo);
Console.WriteLine("{0}",sum);
Console.ReadKey();
}
public static int GetSum(int n1,int n2)
{
int sum = 0;
for (int i = n1; i <=n2; i++)
{
sum += i;
}
return sum;
}
public static int GetNumber(String s)
{
while (true)
{
try
{
int number = Convert.ToInt32(s);
return number;
}
catch
{
Console.WriteLine("输入错误!!!请重新输入");
s = Console.ReadLine();
}
}
}
public static void JudgeNumber( ref int n1,ref int n2)
{
while (true)
{
if (n1 < n2)
{
//符合题意
return;
}
else
{
Console.WriteLine("第一个数字不能大于或者等于第二个数字,请重新输入第一个数字");
string s1 = Console.ReadLine();
//调用Getnumber
n1 = GetNumber(s1);
Console.WriteLine("请重新输入第二个数字");
string s2 = Console.ReadLine();
n2 = GetNumber(s2);
}
}
}
}
}