一個方法是把一些相關的語句組織在一起,用來執(zhí)行一個任務的語句塊。每一個 C# 程序至少有一個帶有 Main 方法的類葵硕。
要使用一個方法需要:
定義方法
調(diào)用方法
class MainClass
{
//----------------方法-------------------//
//方法:就是功能,一個特殊的代碼段
//方法定義格式:
//方法不能嵌套定義贯吓,不能在方法內(nèi)部再去定義方法,但方法可以嵌套調(diào)用
/*返回值類型 方法名稱(參數(shù)列表)
* {
* 方法體(多行代碼)
* }
* 返回值類型:方法返回數(shù)據(jù)的數(shù)據(jù)類
* 參數(shù)列表:我們需要傳個方法的參數(shù)
*
* 根據(jù)參數(shù)和返回值的有無懈凹,將方法分為四類
* 無參數(shù)無返回值方法
* 有參數(shù)無返回值方法
* 無參數(shù)有返回值方法
* 有參數(shù)有返回值方法
* */
//無參數(shù) 無返回值方法
static void PrintNumber()
{
//輸出1-100
for (int i = 1; i <= 100; i++)
Console.Write (i+"\t");
}
//有參數(shù)無返回值方法
//n:形式參數(shù)---形參
static void PrintNumber1(int n)
{
//n 代表傳進來的值
for (int i = 1; i <= n; i++)
Console.Write (i + "\t");
}
//多方法有多個參數(shù)時,多個參數(shù)之間用逗號隔開
//每個參數(shù)必須有數(shù)據(jù)類型
static void PrintNumber2(int start,int end)
{
for (int i = start; i <= end; i++)
Console.Write (i + "\t");
}
static void shuzu(int [] arr)
{
int temp;
int min=0;
for (int i = 0; i < arr.Length-1; i++)
{
min = i;
for (int j = i; j < arr.Length; j++)
{
if (arr [min] > arr [j])
min = j;
}
temp = arr [i];
arr [i] = arr [min];
arr [min] = temp;
}
for (int i = 0; i < arr.Length; i++)
Console.Write (arr [i] + "\t");
}
//無參數(shù)有返回值方法
//由用戶輸出5個正整數(shù)悄谐,返回5個中的最大者
static int GetmaxNumber()
{
int max = -1;
for(int i=1;i<=5;i++)
{
Console.Write("請輸入第{0}個數(shù):",i);
int num = int.Parse(Console.ReadLine());
if (num > max)
max = num;
}
//將return后面表達式的結果介评,返回個方法的調(diào)用者
//結束方法
return max;
}
//有參數(shù)有返回值
//由用戶輸入指定個數(shù)的正整數(shù),返回這些正整數(shù)中的最大者
static int GetmaxNumber1(int count)
{
int max = -1;
for(int i=1;i<=count;i++)
{
Console.Write("請輸入第{0}個數(shù):",i);
int num = int.Parse(Console.ReadLine());
if (num > max)
max = num;
}
return max;
}
//方法間的嵌套調(diào)用
static int GetmaxNumber3()
{
int max = GetmaxNumber1 (5);
return max;
}
public static void Main (string[] args)
{
//無參數(shù)無返回值的調(diào)用
//調(diào)用格式:方法名稱();
//PrintNumber();
//有參數(shù)無返回值的調(diào)用
//調(diào)用格式:方法名稱(實際參數(shù)--實參)
//實參數(shù)據(jù)類型必須和形參類型保持一致
//PrintNumber1(10);
//PrintNumber2 (16,27);
//定義一個方法
//int[] a={6,5,4,3,2,1};
//shuzu(a);
//無參數(shù)有返回值方法的調(diào)用
//調(diào)用格式:變量名 = 方法名稱();
//int max = GetmaxNumber3();
//Console.WriteLine ("the max value is " + max);
//返回值可以不接受,相當于丟失返回值
//有參數(shù)有返回值的調(diào)用
//調(diào)用格式:變量名 = 方法名稱(實際參數(shù)--實參)
//int max=GetmaxNumber1(5);
//Console.WriteLine ("the max value is " + max);
//--------------------------------------結構體-------------------------------------------//
public struct Person
{
public string name;
public string sex;
public int age;
public float height;
}
//定義結構體變量
//結構體類型 結構體類型變量名稱;
//使用默認初始化方法初始化所以成員
Person p = new Person();
//訪問結構體變量成員
//格式:變量名稱.成員名稱;
Console.WriteLine(p.age);
Console.WriteLine (p.name);
p.name = "五更琉璃";
p.age = 14;
p.sex = "女";
p.height = 1.45f;
Console.WriteLine ("name={0},sex={1},age={2},height={3}", p.name, p.sex, p.age, p.height);
Console.WriteLine ();
teacher a = new teacher ();
a.subject = "英語";
a.sex = "男";
a.position = "年級主任";
Console.WriteLine ("subject={0}\nsex1={1}\nposition={2}", a.subject, a.sex, a.position);
//結構體成員還是結構體變量们陆,訪問方式
Person p1 = new Person ();
p1.b.bust = 36f;
p1.b.walst = 30;
p1.b.hips = 36;
//-----------------------------------------枚舉------------------------------------------//
// enum 定義枚舉類型關鍵字
//Season 新的枚舉類型
public enum Season
{
Spring,
Summer,
Autumn,
Winter
}
//每一個枚舉成員都對應一個整形值
//第一個成員默認值為0
//后面每一個成員的值為前一個成員的值+1
//成員的值可以自定義
//自定義成員前面的成員的值保持不變
//后面成員的值為前一個成員的值+1
//定義一種數(shù)據(jù)類型
//一一列舉
//定義枚舉變量:枚舉類型名稱 變量名稱
//枚舉變量的值必須是枚舉類型的成員
Season s = Season.Spring;
Console.WriteLine (s);
s += 1;
Console.WriteLine (s);
//只能是Season成員
//枚舉成員的引用
//枚舉類型名稱.成員名稱
//Season.summer;
switch (s)
{
case Season.Spring:
Console.WriteLine ("Spring");
break;
case Season.Summer:
Console.WriteLine ("Summer");
break;
case Season.Autumn:
Console.WriteLine ("Autumn");
break;
case Season.Winter:
Console.WriteLine ("Winter");
break;
default:
Console.WriteLine ("Error Input");
break;
}
//自定義枚舉類型寒瓦,星期枚舉類型
public enum Week
{
Sunday=0,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday
}
//輸入對應星期,Sunday=0坪仇,Monday=1杂腰,以此類推。輸出對應星期的活動椅文。
Week w=Week.Sunday;
int i;int
//Console.WriteLine (w);
Console.Write("請輸入星期:");
i = int.Parse (Console.ReadLine ());
w += i;
switch (w)
{
case Week.Sunday:
Console.WriteLine ("rest");
break;
case Week.Monday:
Console.WriteLine ("work");
break;
case Week.Tuesday:
Console.WriteLine ("work");
break;
case Week.Wednesday:
Console.WriteLine ("work");
break;
case Week.Thursday:
Console.WriteLine ("work");
break;
case Week.Friday:
Console.WriteLine ("work");
break;
case Week.Saturday:
Console.WriteLine ("rest");
break;
default:
Console.WriteLine ("Error Input");
break;
}
Console.WriteLine ("Hello World!");
}
}
}