程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 函數,

C# 函數,

編輯:C#入門知識

C# 函數,


函數:能夠獨立完成某項功能的模塊。

函數四要素:輸入、輸出、函數體、函數名

函數定義:
(static/public) 返回類型 函數名(參數類型 參數名,參數類型 參數名)
{
函數體
}

函數的調用:
返回變量類型 變量名 = 函數(實參值)


案例:輸入一個數求階乘(寫成函數調用)
/// <summary>
/// 求階乘
/// </summary>
public void Jie()
{
Console.Write("請輸入a=");
int a = int.Parse(Console.ReadLine());
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
Console.Write("階乘結果是:" + jie);
Console.ReadLine();
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要將這個類初始化一下
hanshu.Jie();


可寫成:帶傳值的
/// <summary>
/// 求階乘
/// </summary>
public void Jie(int a)
{
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
Console.Write("階乘結果是:" + jie);
Console.ReadLine();
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要將這個類初始化一下
Console.Write("請輸入a=");
int a = int.Parse(Console.ReadLine());
hanshu.Jie(a);


可以寫成傳值加返回值的:
/// <summary>
/// 求階乘
/// </summary>
public int Jie(int a)
{
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
return jie;
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要將這個類初始化一下
Console.Write("請輸入a=");
int a = int.Parse(Console.ReadLine());

Console.Write("階乘結果是:" + hanshu.Jie(a));
Console.ReadLine();

可以寫成不傳值但是帶返回值的:
/// <summary>
/// 求階乘
/// </summary>
public int Jie()
{
Console.Write("請輸入a=");
int a = int.Parse(Console.ReadLine());
int jie = 1;
for (int i = 1; i <= a; i++)
{
jie *= i;
}
return jie;
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要將這個類初始化一下
Console.Write("階乘結果是:" + hanshu.Jie());
Console.ReadLine();


例:寫一個返回最大值的函數。調用。
/// <summary>
/// 兩個數比較大小返回大的
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public int Max(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}
static void Main(string[] args)
{
Program hanshu = new Program();
//首先需要將這個類初始化一下
Console.WriteLine(hanshu.Max(hanshu.Max(a, b), c));
//函數不僅可以多次使用,還可以嵌套使用

細化

namespace 函數
{
class Program
{
格式1:無參無返
<summary>
累加求和,不需要參數,沒有返回值
</summary>
public void LeiJia()
{
累加求和
Console.Write("請輸入一個正整數:");
int a = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
Console.WriteLine(sum);
Console.ReadLine();
}

 

格式2:無參有返
public int LeiJia1()
{
累加求和
Console.Write("請輸入一個正整數:");
int a = int.Parse(Console.ReadLine());
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}

return sum;
}

 

格式3:有參有返
public int LeiJia2(int a)
{
累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
return sum;
}


格式4:有參無返
public void LeiJia3(int a)
{
累加求和
int sum = 0;
for (int i = 1; i <= a; i++)
{
sum += i;
}
Console.WriteLine(sum);
Console.ReadLine();
}


有參數表示在函數體中不需要再去接收
有返回值表示,我在下文中還需要使用這個結果
在調用函數的時候需要定義一個相同數據類型的變量接收


函數,比較大小返回大的
public double Max(double a, double b)
{
if (a > b)
{
return a;
}
else//a<=b
{
return b;
}
}
函數可以嵌套使用,但是函數不可以嵌套寫

 

public void you()//郵箱,無參無返
{
Console.Write("請輸入郵箱:");
string yx = Console.ReadLine();
if(yx.Contains ("@"))
{
int a=yx.IndexOf ("@");
int b=yx.LastIndexOf ("@");
if (a == b)
{
if(!yx.StartsWith ("@"))
{
string c = yx.Substring(a);
if(c.Contains ("."))
{
string d=yx.Substring (a-1,1);
string e = yx.Substring(a, 1);
if(d!="."&&e!=".")
{
if(!yx.EndsWith ("."))
{
Console.WriteLine("您輸入的郵箱格式正確");
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}

}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console .WriteLine ("郵箱輸入錯誤");
}
Console.ReadLine();
}

 

 

public string you2() //無參有返
{
Console.Write("請輸入郵箱:");
string yx = Console.ReadLine();
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您輸入的郵箱格式正確");
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}

}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}

return yx;
}

有參有返
public string you3(string yx)
{
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您輸入的郵箱格式正確");
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}

}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
return yx;
}

有參無返


public void you4(string yx)
{
if (yx.Contains("@"))
{
int a = yx.IndexOf("@");
int b = yx.LastIndexOf("@");
if (a == b)
{
if (!yx.StartsWith("@"))
{
string c = yx.Substring(a);
if (c.Contains("."))
{
string d = yx.Substring(a - 1, 1);
string e = yx.Substring(a, 1);
if (d != "." && e != ".")
{
if (!yx.EndsWith("."))
{
Console.WriteLine("您輸入的郵箱格式正確");
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}

}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
}
else
{
Console.WriteLine("郵箱輸入錯誤");
}
Console.ReadLine();
}


加10分,給數組中的
public double[] JiaFen(double[] score)
{
for (int i = 0; i < score.Length; i++)
{
score[i] += 10;
}
return score;
}

public int renzong = 0;
public int dianzong = 0;

public string Cai()
{
Console.Write("請輸入您出什麼拳(剪刀、石頭、布):");
string shu = Console.ReadLine();

if (shu == "剪刀" || shu == "石頭" || shu == "布")
{
int ren = 0;
switch (shu)
{
case "剪刀":
ren = 1;
break;
case "石頭":
ren = 2;
break;
case "布":
ren = 3;
break;
}
Random ran = new Random();
int dian = ran.Next(1, 4);
switch (dian)
{
case 1:
Console.WriteLine("電腦出剪刀");
break;
case 2:
Console.WriteLine("電腦出石頭");
break;
case 3:
Console.WriteLine("電腦出布");
break;
}
int jie = ren - dian;
if (jie == 0)
{
return "本輪平局!";
}
else if (jie == 1 || jie == -2)
{
return "本輪勝出!";
}
else
{
return "本輪失敗!";
}
}
else
{
return "輸入有誤!";
}
}
public void tizhong()
{
Console.Write("請輸入性別");
string s = Console.ReadLine();
Console.Write("請輸入體重");
double t = double.Parse(Console.ReadLine());
Console.Write("請輸入身高");
double g = double.Parse(Console.ReadLine());
if (s == "男")
{
double n = t - g + 100;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是標准體重");
}
else if (n > 3)
{
Console.WriteLine("您的體重偏胖");
}
else
{
Console.WriteLine("您的體重偏瘦");
}
}
else if (s == "女")
{
double n = t - g + 110;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是標准體重");
}
else if (n > 3)
{
Console.WriteLine("您的體重偏胖");
}
else
{
Console.WriteLine("您的體重偏瘦");
}
}
else
{
Console.WriteLine("您的輸入有誤");
}
Console.ReadLine();
}

 


static void Main(string[] args)
{
要求:寫一個函數,計算體重是否標准
函數需要三個輸入值,分別為性別、體重kg、身高cm
男:身高-100=體重±3kg
女:身高-110=體重±3kg
Console.Write("請輸入性別");
string s = Console.ReadLine();
Console.Write("請輸入體重");
double t = double.Parse(Console.ReadLine());
Console.Write("請輸入身高");
double g = double.Parse(Console.ReadLine());
if (s == "男")
{
double n = t - g + 100;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是標准體重");
}
else if (n > 3)
{
Console.WriteLine("您的體重偏胖");
}
else
{
Console.WriteLine("您的體重偏瘦");
}
}
else if (s == "女")
{
double n = t - g + 110;
if (n > 3 && n <= -3)
{
Console.WriteLine("您是標准體重");
}
else if (n > 3)
{
Console.WriteLine("您的體重偏胖");
}
else
{
Console.WriteLine("您的體重偏瘦");
}
}
else
{
Console.WriteLine("您的輸入有誤");
}
Console.ReadLine();
Program hanshu = new Program();
hanshu.tizhong();

 

首先對於所在的類進行初始化
Program hanshu = new Program();
hanshu.LeiJia();
int sum = hanshu.LeiJia1();
在這個數值上再加10分
sum += 10;

int sum= hanshu.LeiJia2(5);

Console.WriteLine(sum);
Console.ReadLine();


hanshu.LeiJia3(5);

Console.Write("請輸入a=");
double a = double.Parse(Console.ReadLine());
Console.Write("請輸入b=");
double b = double.Parse(Console.ReadLine());
Console.Write("請輸入c=");
double c = double.Parse(Console.ReadLine());
double max = hanshu.Max( hanshu.Max(a,b),c);

Console.WriteLine(max);
Console.ReadLine();


輸入郵箱賬號,判斷是否格式正確
Program han = new Program();
han.you();


string yx = han.you2( );
Console.WriteLine(yx);
Console.ReadLine();

 

Console.WriteLine("請輸入郵箱地址");
string yx = han.you3(Console .ReadLine ());
Console.WriteLine(yx);
Console.ReadLine();

Console.WriteLine("請輸入郵箱地址");
han.you4(Console .ReadLine ());

 

要求輸入班級人數,根據人數輸入每個人的分數
由於這個班都是少數民族,所以都需要加10分
加10分的過程需要用到函數
Console.Write("請輸入班級人數:");
int a = int.Parse(Console.ReadLine());
double [] score =new double [a];
for (int i = 0; i < a; i++)
{
Console.Write("請輸入第{0}個人的分數:",i+1);
score[i] = double.Parse(Console.ReadLine());
}
Console.WriteLine("所有學生分數輸入完畢,請按回車鍵繼續!");
Console.ReadLine();
初始化
Program hanshu = new Program();
score = hanshu.JiaFen(score);

foreach (double aa in score)
{
Console.WriteLine(aa);
}
Console.ReadLine();

 

猜拳:
人機對戰
剪刀 1
石頭 2
包袱 3
人輸入:(剪刀、石頭、布)
0 平局
1 贏了
-1 輸了
-2 贏了
2 輸了
Console.Write("請輸入您出什麼拳(剪刀、石頭、布):");
string shu = Console.ReadLine();

if (shu == "剪刀" || shu == "石頭" || shu == "布")
{
int ren = 0;
switch (shu)
{
case "剪刀":
ren = 1;
break;
case "石頭":
ren = 2;
break;
case "布":
ren = 3;
break;
}
Random ran = new Random();
int dian = ran.Next(1, 4);
switch (dian)
{
case 1:
Console.WriteLine("電腦出剪刀");
break;
case 2:
Console.WriteLine("電腦出石頭");
break;
case 3:
Console.WriteLine("電腦出布");
break;
}
int jie = ren - dian;
if (jie == 0)
{
Console.WriteLine("本輪平局!");
}
else if (jie == 1 || jie == -2)
{
Console.WriteLine("本輪勝出!");
}
else
{
Console.WriteLine("本輪失敗!");
}
}
else
{
Console.WriteLine("輸入有誤!");
}

Console.ReadLine();

 


輸入過程中若出錯
for (int i = 1; i > 0; i++)
{
Console.Write("請輸入是或者不是!");
string ss = Console.ReadLine();
if (ss == "是" || ss == "不是")
{
break;
}
else
{
Console.WriteLine("輸入有誤!請重新輸入!");
}
}
輸入年月日,查看時間日期格式是否正確

for (int i = 1; i > 0; i++)
{
Console.Write("請輸入年份:");
int year = int.Parse(Console.ReadLine());
if (year >= 0 && year <= 9999)
{
for (int a = 1; a > 0; a++)
{
Console.Write("請輸入月份:");
int month = int.Parse(Console.ReadLine());
if (month >= 1 && month <= 12)
{
for (int b = 1; b > 0; b++)
{
Console.Write("請輸入日期:");
int day = int.Parse(Console.ReadLine());
if (day >= 1 && day <= 31)
{
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
Console.WriteLine("您輸入的日期格式正確,您輸入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day == 31)
{
Console.WriteLine("您輸入的日期錯誤,請重新輸入!");
continue;
}
else
{
Console.WriteLine("您輸入的日期格式正確,您輸入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
}
else
{
if (day <= 28)
{
Console.WriteLine("您輸入的日期格式正確,您輸入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
{
if (day == 29)
{
Console.WriteLine("您輸入的日期格式正確,您輸入的日期是:{0}年{1}月{2}日", year, month, day);
break;
}
else
{
Console.WriteLine("您輸入的日期錯誤,請重新輸入!");
continue;
}
}
else
{
Console.WriteLine("您輸入的日期錯誤,請重新輸入!");
continue;
}
}
}
}
}
else
{
Console.WriteLine("您輸入的日期錯誤,請重新輸入!");
continue;
}
}
a = -10;
}
else
{
Console.WriteLine("您輸入的月份錯誤,請重新輸入!");
continue;
}
}
}
else
{
Console.WriteLine("您輸入的年份錯誤,請重新輸入!");
continue;
}
i = -10;
}Console.ReadLine();

 

for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{
if (j == 3)
{
break;//break跳出最近的循環
}
Console.Write("■");

}
Console.WriteLine();
}


Console.ReadLine();


}
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved