1、我們在Main()函數中,調用Test()函數,我們管Main()函數稱之爲調用者,
管Test()函數稱之爲被調用者。
如果被調用者想要得到調用者的值:
1)、傳遞參數。
2)、使用靜態字段來模擬全局變量。(多個方法都需要使用同一個變量)
語法:在類下面
public static int _number = 10;
如果調用者想要得到被調用者的值:
1)、返回值
class Program
{
////使用靜態字段模擬全局變量
//public static int _number = 3;
static void Main(string[] args)
{
//int a = 3;
//int sum = Sum(a);
//Console.WriteLine(sum);
//Console.ReadKey();
//計算是否是閏年
Console.WriteLine("請輸入一個年份,計算是否是閏年");
int year = Convert.ToInt32(Console.ReadLine());
bool b = IsRun(year);
Console.WriteLine(b);
Console.ReadKey();
}
/// <summary>
/// 接收用戶輸入,計算是否是閏年
/// </summary>
/// <param name="year">用戶輸入的年份</param>
/// <returns>是或否</returns>
public static bool IsRun(int year)
{
bool b = (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
return b;
}
///// <summary>
///// 計算a+5
///// </summary>
///// <param name="a">傳進來的變量a</param>
///// <returns>返回計算後的結果</returns>
//public static int Sum(int a)
//{
// return a = a + 5;
//}
}
2、不管是實參還是形參,都是在內存中開闢了空間的。
3、方法的功能一定要單一。
GetMax(int n1,int n2)
方法中最忌諱的就是出現提示用戶輸入的字眼,除非避免不了的
方法的四個練習
class Program
{
static void Main(string[] args)
{
//1.讀取輸入的整數,定義成方法,多次調用(如果用戶輸入的數字,則返回,否則提示用戶重新輸入)
Console.WriteLine("請輸入一個數字");
string input = Console.ReadLine();
int number = GetNumber(input);
Console.WriteLine(number);
Console.ReadKey();
//2.還記得學循環時做的那道題嗎?只允許用戶輸入y/n,請改成方法
Console.WriteLine("請輸入y/n");
string str = Console.ReadLine();
string strTwo = ShuRu(str);
Console.WriteLine(strTwo);
Console.ReadKey();
//3.查找兩個整數中的最大值:intMax(int i1,int i2)
Console.WriteLine("對比兩個數的最大值");
int i1 = 110;
int i2 = 20;
int max = GetMax(i1, i2);
Console.WriteLine(max);
Console.ReadKey();
//4.計算輸入數組的和:int Sum(int[] values)
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int sum = GetSum(nums);
Console.WriteLine(sum);
Console.ReadKey();
}
#region 第一題的方法
/// <summary>
/// 將用戶輸入轉換成數字並返回
/// 不能轉換要求重新輸。
/// </summary>
/// <param name="i">用戶輸入</param>
/// <returns></returns>
public static int GetNumber(string i)
{
while (true)
{
try
{
int number = Convert.ToInt32(i);
return number;
}
catch
{
Console.WriteLine("輸入的內容不能轉換,請重新輸入");
i = Console.ReadLine();
}
}
}
#endregion
#region 第二題的方法
/// <summary>
/// 接收用戶輸入y/n
/// 如果是就返回,不是就重新輸
/// </summary>
/// <param name="s">接收用戶的輸入</param>
/// <returns></returns>
public static string ShuRu(string s)
{
while (true)
{
if (s != "y" && s != "n")
{
Console.WriteLine("只能是y/n");
s = Console.ReadLine();
}
else
{
return s;
}
}
}
#endregion
#region 第三題的方法
/// <summary>
/// 對比兩個數的最大值並且返回最大值
/// </summary>
/// <param name="n1">第一個整數</param>
/// <param name="n2">第二個整數</param>
/// <returns>返回的最大值</returns>
public static int GetMax(int n1,int n2)
{
return n1 > n2 ? n1 : n2;
}
#endregion
#region 第四題的方法
/// <summary>
/// 計算數組的和並返回
/// </summary>
/// <param name="n">傳入的數組</param>
/// <returns>數組的和</returns>
public static int GetSum(int[] n)
{
int sum = 0;
for (int i = 0; i < n.Length; i++)
{
sum += i;
}
return sum;
}
#endregion
}
4、out、ref、params
1)、out參數。
如果你在一個方法中,返回多個相同類型的值的時候,可以考慮返回一個數組。
但是,如果返回多個不同類型的值的時候,返回數組就不行了,那麼這個時候,
我們可以考慮使用out參數。
out參數就側重於在一個方法中可以返回多個不同類型的值。
class Program
{
static void Main(string[] args)
{
//使用方法計算一個數組的最大值,最小值,總和,平均值
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int[] res = GetMaxMinSumAvg(nums);
Console.WriteLine("最大值是{0},最小值是{1},總和是{2},平均{3}", res[0], res[1], res[2], res[3]);
Console.ReadKey();
}
#region 一次返回多個同類型的值
/// <summary>
/// 求一個數組的最大值,最小值,總和,平均值
/// </summary>
/// <param name="n">傳入的數組</param>
/// <returns></returns>
public static int[] GetMaxMinSumAvg(int[] n)
{
int[] res = new int[4];
//假設:res[0]最大值 res[1]最小值 res[2]總和 res[3]平均值
res[0] = n[0];
res[1] = n[0];
res[2] = 0;
for (int i = 0; i < n.Length; i++)
{
if (n[i] > res[0])
{
res[0] = n[i];
}
if (n[i] < res[1])
{
res[1] = n[i];
}
res[2] += n[i];
}
res[3] = res[2] / n.Length;
return res;
}
#endregion
}
out參數的使用
class Program
{
static void Main(string[] args)
{
//out參數的調用
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int max1;
int min1;
int sum1;
int avg1;
string s1;
bool b1;
double d1;
Test(nums, out max1, out min1, out sum1, out avg1, out s1, out b1, out d1);
Console.WriteLine("最大值是{0},最小值是{1},總和是{2},平均{3}", max1, min1, sum1, avg1);
Console.WriteLine(s1);
Console.WriteLine(b1);
Console.WriteLine(d1);
Console.ReadKey();
}
#region out參數的使用
/// <summary>
/// 計算一個數組的最大值,最小值,平均值,總和,在返回一個string類型,bool類型,double類型
/// </summary>
/// <param name="n">求值的數值</param>
/// <param name="max">最大值</param>
/// <param name="min">最小值</param>
/// <param name="sum">總和</param>
/// <param name="avg">平均值</param>
/// <param name="s">返回的string類型</param>
/// <param name="b">返回的bool類型</param>
/// <param name="d">返回的double類型</param>
public static void Test(int[] n,out int max,out int min,out int sum,out int avg, out string s, out bool b, out double d)
{
//out參數要求在方法的內部必須爲其賦值
max = n[0];
min = n[0];
sum = 0;
for (int i = 0; i < n.Length; i++)
{
if (n[i] > max)
{
max = n[i];
}
if (n[i] < min)
{
max = n[i];
}
sum += n[i];
}
avg = sum / n.Length;
s = "abc";
b = true;
d = 3.13;
}
#endregion
}
out參數:方法內必須賦值,方法外不用,因爲在方法內已經賦值了
class Program
{
static void Main(string[] args)
{
/*
分別提示用戶輸入用戶名和密碼,寫一個方法來判斷用戶輸入的是否正確
返回給用戶一個登陸結果,並且還要單獨的返回給用戶一個登陸信息
如果用戶名錯誤,除了返回登良路結果外,還要返回“用戶名錯誤”,密碼也一樣
*/
Console.WriteLine("請輸入用戶名");
string userName = Console.ReadLine();
Console.WriteLine("請輸入密碼");
string userPwd = Console.ReadLine();
string msg1;
bool b = IsLogin(userName, userPwd, out msg1);
Console.WriteLine("登陸信息:{0}", b);
Console.WriteLine("登陸結果:{0}", msg1);
Console.ReadKey();
}
#region 登陸方法練習
/// <summary>
/// 判斷登陸結果
/// </summary>
/// <param name="name">用戶名</param>
/// <param name="pass">密碼</param>
/// <param name="msg">登陸信息</param>
/// <returns>返回登陸結果</returns>
public static bool IsLogin(string name, string pass, out string msg)
{
if (name == "admin" && pass == "888888")
{
msg = "登陸成功";
return true;
}
else if (name == "admin")
{
msg = "密碼錯誤";
return false;
}
else if (pass == "888888")
{
msg = "用戶名錯誤";
return false;
}
else
{
msg = "全都錯誤";
return false;
}
}
#endregion
}
//使用方法實現TryParse
class Program
{
static void Main(string[] args)
{
Console.WriteLine("請輸入一個字符串,轉爲數字");
string num = Console.ReadLine();
int num1;
bool b = TryParse(num, out num1);
Console.WriteLine(num1);
Console.WriteLine(b);
Console.ReadKey();
}
public static bool TryParse(string n,out int result)
{
result = 0;
try
{
result = Convert.ToInt32(n);
return true;
}
catch
{
return false;
}
}
}
2)、ref參數
能夠將一個變量帶入一個方法中進行改變,改變完成後,再講改變後的值帶出方法。
ref參數要求在方法外必須爲其賦值,而方法內可以不賦值。
class Program
{
static void Main(string[] args)
{
//ref參數調用
int n1 = 10;
int n2 = 20;
JiaoHuan(ref n1, ref n2);
Console.WriteLine(n1);
Console.WriteLine(n2);
Console.ReadKey();
}
public static void JiaoHuan(ref int n1, ref int n2)
{
int temp = n1;
n1 = n2;
n2 = temp;
}
}
3)、params可變參數
將實參列表中跟可變參數數組類型一致的元素都當做數組的元素去處理。
params可變參數必須是形參列表中的最後一個元素。
class Program
{
static void Main(string[] args)
{
// int[] s = { 99, 98, 97 };
Test("張三", 1,99, 98, 97);
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);
}
}
class Program
{
static void Main(string[] args)
{
//求任意長度數組的和,整數類型
int[] nums1 = { 1, 2, 3 };
int sum = Nums(1, 2, 3, 4, 5, 6, 7, 8, 9);
Console.WriteLine(sum);
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);
}
public static int Nums(params int[] ns)
{
int sum = 0;
for (int i = 0; i < ns.Length; i++)
{
sum += ns[i];
}
return sum;
}
}
5、方法的重載
概念:方法的重載指的就是方法的名稱相同給,但是參數不同。
參數不同,分爲兩種情況
1)、如果參數的個數相同,那麼參數的類型就不能相同。
2)、如果參數的類型相同,那麼參數的個數就不能相同。
***方法的重載跟返回值沒有關係。
class Program
{
static void Main(string[] args)
{
string n = "姓";
string n1 = "名";
M(n,n1);
}
public static string M(string name,string waihao)
{
return name + waihao;
}
public static void M(int n1, int n2, int n3)
{
int result = n1 + n2 + n3;
}
public static double M(double d1, double d2)
{
return d1 + d2;
}
}
6、方法的遞歸
方法自己調用自己。
需要有一個條件,來停止調用
class Program
{
//定義一個全局字段來作爲停止調用的條件,反正方法內,會被重置
public static int i = 0;
static void Main(string[] args)
{
Test();
Console.ReadKey();
}
public static void Test()
{
Console.WriteLine("從前有座山");
Console.WriteLine("山裏有座廟");
Console.WriteLine("廟裏有一個老和尚跟一個小和尚");
Console.WriteLine("老和尚對小和尚講故事");
i++;
if (i >= 10)
{
return;
}
Test();
}
}