为什么要使用方法?
因为某些常用的代码,在不停的出现执行。所以要把这些经常使用的代码,封转成一个方法
方法是什么?
函数就是将一堆代码进行重用的一种机制
方法的定义。
[访问修饰符] [static ] 返回值类型
方法名([参数列表])
{
方法体;
}
注意:我用[]标记的,表示可以省略
方法的访问修饰符:
访问修饰符 | 说明 |
public | 公有访问。不受任何限制。 |
private | 私有访问。只限于本类成员访问,子类,实例都不能访问。 |
protected | 保护访问。只限于本类和子类访问,实例不能访问。 |
internal | 内部访问。只限于本项目内访问,其他不能访问。 |
protected internal | 内部保护访问。只限于本项目或是子类访问,其他不能访问 |
static:静态的。被static标记的方法叫做静态方法,没有被static 标记的方法叫做实例方法,咱们现阶段学习的都是静态方法
返回值类型:这个方法要返回去的值的类型 如果是void 就表示没有返回值
方法名:随便起,但是要符合Pascal(每个单词的首字母要大写,其余字母要小写)
参数列表:传给这个方法的参数
():不能省略,不管有木有参数,都不能省略
方法的调用
在静态方法中调用:类名.方法名();如果在当前类中,调用当前类中的方法,那么类名可以省略
咱们写的方法,要想这个方法被执行,就必须放到Main()函数里面调用。
使用方法
就是为了让方法帮助咱们做一件事儿,
参数:为了实现这个方法的功能,我们必须要给这个方法的东东
返回值:就是我要的东西
咱们经常使用的方法:
Console.WriteLine();
Console.ReadLine();
Console.ReadKey();
int.Parse();
Convert.ToInt32();
3、局部变量
局部变量的定义:
局部变量的作用域:
4、 被调用者想得到调用者里面的变量的值:
1)一种 来一个字段
注意:这个字段不能定义到方法里面,要定义到方法外面,比如类里面(静态方法中,只能访问到静态字段)
2)加个参数
直接把值传进来就ok了
调用者想得到被调用者的值:
之后做了很多的练习
读取输入的整数,定义成方法,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
static void Main(string[] args)
{
//1
读取输入的整数,定义成方法,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
//要给用户返回一个int类型的值
Console.WriteLine("请输入一个数字");
string str = Console.ReadLine(); //用户输入的字符串
在方法中进行转换
int number =
Number(str);
Console.WriteLine("转换成功,您刚刚输入的数字是{0}",number);
Console.ReadKey();
}
///
///
接收用户输入的一个字符串,判断是否可以转换为int类型的数字
///
///
接收一个string类型的字符串进行判断
///
成功返回一个int类型的值
public static int
Number(string str) //接收用户输入的是个string类型的
而返回的是个int类型多的
{
while
(true)
{
try
{
int number = Convert.ToInt32(str);
//开始转换
return number; //转换成功以后
返回一个值
}
catch
{
Console.WriteLine("转换失败,请重新输入");
//提示用户转换失败
str =
Console.ReadLine();
//并重新接收
}
}
}
/2
还记得学循环时做的那道题吗?只允许用户输入y或n,请改成方法
static void
Main(string[] args)
{
//2
还记得学循环时做的那道题吗?只允许用户输入y或n,请改成方法
Console.WriteLine("你确定要关机吗
[Y/N]");
string answer =
Console.ReadLine();
string an=
Answer(answer);
string a= an== "Y" ? "正在关机" :
"取消操作";
Console.WriteLine("您的状态是{0}",a);
Console.ReadKey();
}
///
///
只允许用输入[Y/N]的判断,无返回值
///
///
public static string
Answer(string answer)
{
while
(true)
{
if (answer == "Y" || answer ==
"N")
{
return
answer;
}
else
{
Console.WriteLine("输入有误,请重新输入");
Console.WriteLine("你确定要关机吗
[Y/N]");
answer =
Console.ReadLine();
}
}
}
查找两个整数中的最大值:int Max(int i1,int
i2)
static void Main(string[] args)
{
//3查找两个整数中的最大值:int Max(int i1,int
i2)
Console.WriteLine("请输入一个数字");
int number1 =
Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入第二个数字");
int number2 = Convert.ToInt32(Console.ReadLine());
int
max=Max(number1,
number2);
Console.WriteLine("两个数当中,比较大的一个是{0}",max);
Console.ReadKey();
}
///
///
查找两个数当中最大的一个数字
///
///
第一个数字
///
第二个数字
///
public static int Max(int
number1,int number2)
{
return
number1 > number2 ? number1 :
number2;
}
计算输入数组的和
static void Main(string[] args)
{
//4计算输入数组的和:int Sum(int[]
values)
int [] numbers = {1,2,3,4,5,6,7,8,9,10
};
int
sum=Sum(numbers); //把数组放到方法里边
来进行计算
Console.WriteLine("数组的和是{0}",sum);
Console.ReadKey();
}
///
///
计算一个int类型数组的和
///
///
存放一个int类型的数组
///
public static int Sum(int[]
numbers)
//要返回一个和,所以,有返回值
{
int sum
= 0;
//定义一个变量来接收总和
for (int i = 0; i < numbers.Length;
i++)
{
sum += numbers[i];
//sum等于数组中
每个元素相加后的结果
}
return
sum; ;
}
//1、编写一段程序,运行时向用户提问“你考了多少分?(0~100)”,
//接受输入后判断其等级并显示出来。判断依据如下:等级={优 (90~100分);
//良 (80~89分);中 (60~69分);差
(0~59分);
int score;
Console.WriteLine("你考了多少分");
string strScore =
Console.ReadLine();
while
(true)
{
try
{
score =
Convert.ToInt32(strScore);
if (score > 100 || score <
0)
{
Console.WriteLine("请输入正确范围内的成绩");
strScore
=Console.ReadLine();
}
else
{
break;
}
}
catch
{
Console.WriteLine("请输入正确的成绩");
strScore =
Console.ReadLine();
}
}
string level=
Level(score);
Console.WriteLine("你的等级是{0}",level);
Console.ReadKey();
}
public static string Level(int
score)
{
string
str = "";
if (score >= 90
)
{
str = "优";
}
else if
(score >=
80)
{
str = "良";
}
else if
(score >=
70)
{
str = "中";
}
else
{
str = "差";
}
return
str;
}
第二种方式
static void Main(string[]
args)
{
Console.WriteLine("请输入一个数字");
string strScore =
Console.ReadLine();
int score =
0;
while
(true)
{
try
{
score =
Convert.ToInt32(strScore);
if (score > 100 || score <
0)
{
Console.WriteLine("请输入正确范围的成绩");
strScore =
Console.ReadLine();
continue;
}
else
{
break;
}
}
catch
{
Console.WriteLine("输入有误,请重新输入");
strScore =
Console.ReadLine();
}
}
string
strLevel =
Level(score);
Console.WriteLine("这次考试成绩的等级是{0}",strLevel);
Console.ReadKey();
}
public static string Level(int
score)
{
string
str = "";
switch (score /
10)
{
case
10:
case
9:
str =
"优";
break;
case
8:
str =
"良";
break;
case
7:
str =
"中";
break;
default:
str =
"差";
break;
}
return
str;//把等级返回
}
2、
编写一个程序,输出所有的水仙花数
(水仙花数为一个三位数,他各个位数上的立方和等于该数本身)
static void Main(string[] args)
{
string str=
ShuiXianHua();
Console.WriteLine(str);
Console.ReadKey();
}
public static string
ShuiXianHua()
{
string
str="";
for (int i = 100; i <=999;
i++)
{
int bai = i /
100;
int shi = i % 100 /
10;
int ge = i %
10;
int sum = bai * bai * bai + shi * shi * shi + ge * ge *
ge;
if (sum ==
i)
{
str += i + "
";
}
}
return
str;
}
//7、求数组的最大值、最小值
static void Main(string[] args)
{
int[]
numbers = {1,2,3,4,5,6,7,8,9
};
int
max=Max(numbers);
int min =
Min(numbers);
Console.WriteLine("数组中的最大值是{0},最小值是{1}",max,min);
Console.ReadKey();
}
///
///
判断一个int类型数组中的最大值
///
///
一个int类型的数组
///
最大值
public static int Max(int[]
numbers)
{
int max
=
numbers[0];
for (int i = 0; i < numbers.Length;
i++)
{
if (max <
numbers[i])
{
max =
numbers[i];
}
}
return
max;
}
///
///
判断一个int类型数组中的最小值
///
///
一个int类型的数组
///
最小值
public static int Min(int[]
numbers)
{
int min
=
numbers[0];
for (int i = 0; i < numbers.Length;
i++)
{
if (min >
numbers[i])
{
min = numbers[i];
}
}
return
min;
}
//8、求两个数的和
static void Main(string[] args)
{
Console.WriteLine("请输入数字A");
int numberA =
Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入数字B");
int numberB = Convert.ToInt32(Console.ReadLine());
int
sum=Sum(numberA,
numberB);
Console.ReadKey();
}
///
///
求两个数的和
///
///
第一个数字
///
第二个数字
///
返回一个和
public static int Sum(int
numberA,int numberB)
{
return
numberA + numberB;
}
//为教师编写一个程序,
该程序使用一个数组存储30个学生的考试成绩,并给各个数组元素指定一个1-100的随机值,然后计算平均成绩。
static void Main(string[] args)
{
int[]
score = new
int[30];
int
avg=Avg(score);
Console.WriteLine("平均成绩是{0}分",avg);
Console.ReadKey();
}
public static int Avg(int []
numbers)
{
Random r
= new Random();
//定义一个随机数
int sum =
0;
//
for
(int i = 0; i < numbers.Length;
i++)
{
numbers[i] = r.Next(1,
101);
sum +=
numbers[i];
}
return sum / numbers.Length;
}
原文:http://www.cnblogs.com/fight2014/p/3637103.html