---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------
方法(函数)
[访问修饰符] [static] 返回值类型 方法名([参数])
{
方法体;
}
注意:1)一般情况下,方法一般要定义在类中。(对于静态方法的调用,如果在同一个类中,可以直接写名字调用)
2)如果方法没有返回值,则返回值类型写void。
3)如果方法没有参数,()不能省略。
方法中最好不要提示用户输入。
在方法中定义的变量,称为局部变量,其作用域从定义开始,到其所在的大括号结束为止。
在方法名后面的括号内定义变量,叫做这个方法的参数。在这里定义的变量用于接收调用者传过来的数据。
注意:如果一个方法一旦有参数,那么调用者就必须传参数,并且传参数的个数与对应位置上的类型必须一致。
常用的学过的一些方法
Console.WriteLine(); Console.Write(); Console.ReadLine();
Console.ReadKey(); Convert.ToInt32(); int.Parse();
当调用者想访问我们方法中的变量时,可以通过返回值返回
例如:string s = Console.ReadLine();
int i = Convert.ToInt32("22");
为什么方法前面能够定义一个变量收到方法的值呢,这是因为在方法中使用了返回值,只要在方法中返回了值,那么在调用方法中,前面就应该用一个变量来接收方法的返回值。
注意:一个方法只有一个返回值。
一个变量一旦定义在方法外,类的里面,就叫做类的字段。这个变量可以被本类中所有方法访问,但静态方法只能访问静态字段。
static void Main(string[] args)
{
ShowUI();
Console.ReadLine();
int[] score = new int[] { 18, 46, 54, 168, 1647, 1354, 465, 138, 165, 3587 };
for (int i = 0; i < score.Length; i++)
{
for(int j=0;j<score.Length-i-1;j++)
{
if (score[j] > score[j + 1])
{
int temp=score[j];
score[j]=score[j+1];
score[j + 1] = temp;
}
}
}
for (int i = 0; i < score.Length; i++)
{
Console.WriteLine(score[i]);
}
Console.ReadKey();
ShowUI();
Console.ReadLine();
Console.WriteLine("谢谢使用,按任意键退出程序");
Console.ReadKey();
}
/// <summary>
/// 用于显示软件主界面的一个方法
/// </summary>
public static void ShowUI()
{
Console.WriteLine("************************************");
Console.WriteLine(" 欢迎你使用本软件 ");
Console.WriteLine("************************************");
}
static void Main(string[] args)
{
//int year = Convert.ToInt32(Console.ReadLine());
int year = int.Parse(Console.ReadLine());
bool result = Answer(year);
if (result)
{
Console.WriteLine("是闰年");
}
else
{
Console.WriteLine("不是闰年");
}
Console.ReadKey();
}
public static bool Answer(int year)
{
if (year % 400 == 0 || year % 4 == 0 && year % 100 != 0)
{
return true;
}
else
{
return false;
}
}
---------------------- ASP.Net+Android+IO开发S、.Net培训、期待与您交流! ----------------------