一、方法:做一件事情的办法
王者荣耀 怎样玩王者?
先注册账号 登录 选角色 选服务器 组队开战
乔碧罗 我想在飞鱼上开一个直播间,怎么开?
先注册账号 登录 认证 开始直播
为什么要用方法:是为了方便代码重用
语法:
[访问修饰符] static 返回值类型 方法名称(参数列表)
{
//要执行的代码
}
访问修饰符:public private protected
static:代表这个方法是静态方法
返回值类型:int double string string[],如果不需要返回值,必须要写上void
方法名称:Pascal命名规范 每个单词的首字母均要大写
参数列表:可以是任何类型,如果没有参数,小括号也不允许省略
方法写好以后,如果想要被执行,必须放到main函数中
方法调用的时候,如果该方法有参数,只需要将对应类型的变量名称给它就可以,不需要在参数中写变量类型。
调用方法:类名.方法名(); 在当前类中,可以省略类名。
定义函数:定义的时候要把函数放在类的内部,不能放到函数的内部,也就说函数中不能嵌套函数
public static int GetMax(int n1, int n2) //参数叫形参,形式上的参数
{
int max = n1 > n2 ? n1 : n2;
return max;
}
调用函数:
GetMax(2,3);
zuiDaZhi = Program.GetMax(x1, x2); //实参:实际上的参数
形参和实参都是在内存中开辟了空间的
- 方法当中忌讳出现让用户输入的字眼,即在方法中不要调用Console.ReadLine()
练习一:求两个整数中的最大值
练习二:读取用户输入的整数,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
练习三:只能让用户输入yes或no,只要不是,就重新输入
练习四:告诉我你叫什么,我能知道你上辈子干啥的?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day7zy_machenxi
{
class Program
{
static void Main(string[] args)
{
#region 练习四:告诉我你叫什么,我能知道你上辈子干啥的?
Console.WriteLine("练习四:你叫什么名字?");
string name = Console.ReadLine();
SuanMing(name);
#endregion
#region 练习三:只能让用户输入yes或no,只要不是,就重新输入
Console.WriteLine("\n\n练习三:请输入yes或no:");
string answer = Console.ReadLine();
while (!YesNo(answer))
{
Console.WriteLine("输入错误,请重新输入yes或no:");
answer = Console.ReadLine();
}
#endregion
#region 练习二:读取用户输入的整数,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
Console.WriteLine("\n\n练习二:请输入一个整数:");
string strAge = Console.ReadLine();
int age = GetNumber(strAge); //调用方法 strAge:实参
Console.WriteLine("你输入的数字是:{0}", age);
#endregion
#region 练习一:计算两个整数中的最大值,并返回
Console.WriteLine("\n\n练习一:\n请输入第一个数:");
int x1 = Convert.ToInt32(Console.ReadLine()); //第一个数
Console.WriteLine("请输入第二个数:");
int x2 = Convert.ToInt32(Console.ReadLine()); //第二个数
Console.WriteLine("{0}和{1}中最大的是{2}", x1, x2, GetMax(x1,x2));
#endregion
Console.Read();
}
//1.求两个整数中的最大值
public static int GetMax(int n1, int n2)
{
int max = n1 > n2 ? n1 : n2;
return max;
}
//2.读取用户输入的整数,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
public static int GetNumber(string str)
{
while (true)
{
try
{
int number; //存储转换成的数字
number = int.Parse(str); //转换成数字
return number;
}
catch (Exception)
{
Console.WriteLine("输入的不是数字,请重新输入!");
str = Console.ReadLine();
}
}
}
//3.判断用户输入的是否是yes或no
public static bool YesNo(string s)
{
bool yn = false;
if (s == "yes" || s == "no")
{
yn = true;
}
return yn;
}
//4.告诉我你叫什么,我能知道你上辈子干啥的?
public static void SuanMing(string name)
{
switch (name)
{
case "雪娇":
Console.WriteLine("怡红院头牌");
break;
case "老马":
Console.WriteLine("怡红院老鸨");
break;
case "老杨":
Console.WriteLine("怡红院辅导员");
break;
case "乔碧罗:":
Console.WriteLine("潘金莲转世");
break;
default:
Console.WriteLine("条子");
break;
}
}
}
}
-
值类型与引用类型
值类型:int double float 枚举 结构体 bool decimal
引用类型:数组 字符串
值类型与引用类型的区别:值类型是存储在栈上的,引用类型内存分配在堆上
当在方法中调用值类型的参数时,只是把该变量中存放的数值拷贝一份给了方法,该变量本身不会有任何变化。
当在方法中调用引用类型的参数时,是把该变量的引用传进了方法中,在方法中对引用类型做的任何改变,都会
发生在该变量上。
引用类型可以这么理解:当传递引用类型的时候,是把引用类型他们家的钥匙传递给了方法。
二、out ref params
1.out:把值类型做成引用类型进行传递
out关键字:能把值类型的变量作为引用类型来传递
out参数在方法内部必须赋值,在方法外可以赋值,也可以不赋值。
2.ref
把变量带到方法中改变,改变完以后,再将改变的值带出方法。
ref与out区别:ref在方法外必须赋值,out在方法内赋值。
3.params:可变参数
注意事项:params参数只能放在最后一个参数
当调用参数的时候,会讲普通参数之外的所有与params参数相同类型的参数当做最后一个参数来处理
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day8_machenxi
{
class Program
{
static void Main(string[] args)
{
#region 练习一、用out参数传递一个int变量,打印在方法中的值和执行完方法以后的值。
Console.WriteLine("\n练习一:用out参数传递一个int变量");
int num;
outTest(out num);
Console.WriteLine("方法调用完以后,n变成了{0}", num);
#endregion
#region 练习二、传入一个数组,使用out参数得到该数组的最大值、最小值、平均值、和。
Console.WriteLine("\n练习二:传入一个数组,使用out参数得到该数组的最大值、最小值、平均值、和");
int[] arr = { 23, 21, 17, 12, 15, 8, 9 };
int max;
int min;
int sum;
double ave;
GetWithOut(arr, out max, out min, out ave, out sum);
Console.WriteLine("数组中最大值为{0},最小值为{1},平均值为{2},和为{3}", max,
min, ave, sum);
#endregion
#region 练习三:例:分别提示用户输入用户名和密码,写一个方法判断用户输入的是否正确,返回给用户一个登陆结果,
//并且还要单独返回给用户一个登陆信息: 如果用户名错误,除了返回登陆结果之外,还要返回一个用户名错误 或密码错误
Console.WriteLine("\n练习三:输入用户名和密码(aaa 111)");
Console.WriteLine("请输入用户名:");
string username = Console.ReadLine();
Console.WriteLine("请输入密码:");
string pwd = Console.ReadLine();
returnUnPwd(username, pwd);
#endregion
#region 练习四:用ref参数传递一个int变量,打印在方法中的值和执行完方法以后的值。
Console.WriteLine("\n练习四:用ref参数传递一个int变量");
int age = 21;
refTest(ref age);
Console.WriteLine("调用完方法以后,变量的值为:{0}", age);
#endregion
#region 练习五:使用params参数计算学生的总成绩
Console.WriteLine("\n练习五:使用params参数计算学生的总成绩");
Console.WriteLine("请输入你的姓名:");
string name = Console.ReadLine(); //姓名
int ch = 40;
int mt = 80;
int en = 6;
int history = 98;
double biology = 78.5;
//调用方法计算总成绩
int result = GetTotalScore(name, ch, mt, en, history, (int)biology);
Console.WriteLine("{0}的成绩为{1}",name,result);
#endregion
Console.Read();
}
//1.用out参数传递一个int变量
public static void outTest(out int n)
{
n = 10;
Console.WriteLine("在方法内,变量n为{0}", n);
}
//2.传入一个数组,使用out参数得到该数组的最大值、最小值、平均值、和。
public static void GetWithOut(int[] arr, out int max, out int min, out double avg, out int sum)
{
//out参数必须要在方法内部赋值
max = arr[0];
min = arr[0];
avg = 0;
sum = 0;
for (int i = 0; i < arr.Length; i++)
{
max = max > arr[i] ? max : arr[i];
min = min < arr[i] ? min : arr[i];
sum += arr[i];
}
avg = sum / arr.Length;
}
//3.返回用户名和密码结果
public static void returnUnPwd(string a,string b)
{
if (a == "aaa" && b == "111")
{
Console.WriteLine("登陆成功!\n您的用户名是:{0}\n您的密码是:{1}",a,b);
}
else if (a != "aaa")
{
Console.WriteLine("登录失败!\n您的用户名错误。");
}
else
{
Console.WriteLine("登录失败!\n您的密码错误。");
}
}
//4.用ref参数传递一个int变量
public static void refTest(ref int n)
{
n += 10;
Console.WriteLine("在方法的内部,变量值为:{0}", n);
}
//5.统计学生的总成绩
public static int GetTotalScore(string name, params int[] scores)
{
int sum = 0; //存储总成绩
for (int i = 0; i < scores.Length; i++)
{
sum += scores[i];
}
return sum;
}
}
}
三、方法的重载
特点:方法的名字相同,参数不同,参数不同分为两种,一种是参数个数不同,另一种是参数的类型不同
public static int GetMax(int n1, int n2)
{
int max = n1 > n2 ? n1 : n2;
return max;
}
public static double GetMax(double d1, double d2) //与第一个相比,参数的类型不同
{
double max = d1 > d2 ? d1 : d2;
return max;
}
public static double GetMax(int n1, int n2, int n3) //参数的个数不同
{
return n1;
}
四、函数的递归:方法调用方法本身
练习六:提示用户输入两个数字,计算这两个数字间所有整数的和
练习七:用方法实现:有一个字符串数组,求出字符串中最长的字符串
练习八:用方法实现:求整数数组的平均值,保留两位小数
练习九:写一个方法,判读用户输入的数字是不是质数(只能被1和本身整除的数字)
练习十:用方法实现:接收用户输入然后判断等级并显示出来 判断依据:等级=优(90-100);良(80-89) 中(70-79) 差(60以下)
练习十一:翻转字符串数组(例:strA={"中国",“美国”,“巴西”,“荷兰”},翻转后为{“荷兰”,“巴西”,“美国”,“中国”})
练习十二:写一个方法,计算圆形的周长和面积()
练习十三:用方法实现:用冒泡排序,对数组进行升序排列
练习十四:用方法实现:将一个字符串数组分割成|形式的字符串
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Day8_machenxi
{
class Program
{
static void Main(string[] args)
{
#region 练习十四:用方法实现:将一个字符串数组分割成|形式的字符串
Console.WriteLine("练习十四:用方法实现:将一个字符串数组分割成|形式的字符串");
string[] strsCut = { "a", "b", "c", "d", "e" };
Console.Write("分割前的字符串为:");
foreach (string s1 in strsCut)
{
Console.Write(s1);
}
Console.WriteLine();
cutOff(strsCut);
#endregion
#region 练习十三:用方法实现:用冒泡排序,对数组进行升序排列
Console.WriteLine("\n练习十三:用方法实现:用冒泡排序,对数组进行升序排列");
int[] BubbleInts = { 3, 2, 1, 6, 5, 4 };
Console.WriteLine("冒泡排序前:3 2 1 6 5 4");
Bubble(BubbleInts);
#endregion
#region 练习十二:写一个方法,计算圆形的周长和面积
Console.WriteLine("\n练习十二:写一个方法,计算圆形的周长和面积");
Console.WriteLine("请输入圆形的半径:");
int r = int.Parse(Console.ReadLine());
LenArea(r);
#endregion
#region 练习十一:翻转字符串数组
Console.WriteLine("\n练习十一:翻转字符串数组");
string[] strReverse = { "中国","美国","巴西","荷兰"};
Console.Write("翻转前的字符串为:");
for (int i = 0; i < strReverse.Length; i++)
{
Console.Write("{0} ", strReverse[i]);
}
Console.WriteLine();
Reverse(strReverse);
#endregion
#region 练习十:用方法实现:接收用户输入然后判断等级并显示出来
Console.WriteLine("\n练习十:用方法实现:接收用户输入然后判断等级并显示出来");
Console.WriteLine("你考了多少分?(0-100)");
int score = int.Parse(Console.ReadLine());
returnLevel(score);
#endregion
#region 练习九:写一个方法,判读用户输入的数字是不是质数(只能被1和本身整除的数字)
Console.WriteLine("\n练习九:写一个方法,判读用户输入的数字是不是质数(只能被1和本身整除的数字)");
Console.WriteLine("请输入一个数字:");
int nump = int.Parse(Console.ReadLine());
IsPrime(nump);
#endregion
#region 练习八:用方法实现:求整数数组的平均值,保留两位小数
Console.WriteLine("\n练习八:用方法实现:求整数数组的平均值,保留两位小数");
int[] ints = { 5, 3, 6, 2 };
Console.WriteLine("这个整型数组为:5, 3, 6, 2");
Averageints(ints);
#endregion
#region 练习七:用方法实现:有一个字符串数组,求出字符串中最长的字符串
Console.WriteLine("\n练习七:用方法实现:有一个字符串数组,求出字符串中最长的字符串");
String[] strs = { "ab", "abcd", "abc" };
Console.WriteLine("这个字符串数组为:ab,abcd,abc");
Maxstringlength(strs);
#endregion
#region 练习六:提示用户输入两个数字,计算这两个数字间所有整数的和
Console.WriteLine("\n练习六:提示用户输入两个数字,计算这两个数字间所有整数的和");
Console.WriteLine("请输入第一个数字:");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第二个数字:");
int num2 = int.Parse(Console.ReadLine());
IntSum(num1, num2);
#endregion
Console.Read();
}
//14.将一个字符串数组分割成|形式的字符串
static void cutOff(string[] s)
{
for(int i = 0; i < s.Length-1; i++)
{
s[i] = s[i] + "|";
}
Console.Write("分割后的字符串为:");
foreach(string s1 in s)
{
Console.Write(s1);
}
Console.WriteLine();
}
//13.用冒泡排序,对数组进行升序排列
static void Bubble(int[] a)
{
int z = 0;
for (int i = 0; i < a.Length; i++)
{
for (int j = 0; j < a.Length - i - 1; j++)
{
if (a[j] > a[j + 1])
{
z = a[j + 1];
a[j + 1] = a[j];
a[j] = z;
}
}
}
Console.Write("冒泡排序后:");
for (int i = 0; i < a.Length; i++)
{
Console.Write("{0} ", a[i]);
}
Console.WriteLine();
}
//12.计算圆形的周长和面积
static void LenArea(int r)
{
Console.WriteLine("半径为{0}的圆周长为{1},面积为{2}。", r, 2 * 3.14 * r,3.14*r*r);
}
//11.翻转字符串数组
static void Reverse(string[] strReverse)
{
string ReverseTemp;
for (int i = 0; i < strReverse.Length / 2; i++)
{
ReverseTemp = strReverse[i];
strReverse[i] = strReverse[strReverse.Length - i - 1];
strReverse[strReverse.Length - i - 1] = ReverseTemp;
}
Console.Write("翻转后的字符串为:");
for (int i = 0; i < strReverse.Length; i++)
{
Console.Write("{0} ", strReverse[i]);
}
Console.WriteLine();
}
//10.接收用户输入然后判断等级并显示出来
static void returnLevel(int score)
{
if (score >= 90)
{
Console.WriteLine("等级为:A");
}
else if (score >= 80)
{
Console.WriteLine("等级为:B");
}
else if (score >= 60)
{
Console.WriteLine("等级为:C");
}
else
{
Console.WriteLine("等级为:D");
}
}
//9.判断是不是质数
static void IsPrime(int n)
{
bool flag = true;
for (int j = 2; j < Math.Sqrt(n); j++)
{
if (n % j == 0)
{
flag = false;
}
}
if (flag)
{
Console.WriteLine("{0}是质数。",n);
}
else
{
Console.WriteLine("{0}不是质数。", n);
}
}
//8.计算出一个整型数组的平均值。要求:精确到两位小数
static void Averageints(int[] a)
{
double sumints = 0;
for (int i = 0; i < a.Length; i++)
{
sumints += a[i];
}
//string aver = sumints.ToString("#0.00");
double aver = Math.Round(sumints / a.Length, 2);
Console.WriteLine("这个整型数组的平均值为:{0}", aver);
}
//7.字符串数组中找出最长的字符串
static void Maxstringlength(string[] a)
{
int maxlength = 0;
string max = "";
for (int i = 0; i < a.Length; i++)
{
if (a[i].Length > maxlength)
{
maxlength = a[i].Length;
max = a[i];
}
}
Console.Write("最长的字符串为:");
Console.WriteLine(max);
}
//6.计算这两个数字间所有整数的和
static void IntSum(int a,int b)
{
int sum = 0;
if (a > b)
{
int t = a;
a = b;
b = t;
}
for(int i = a; i <= b; i++)
{
sum += i;
}
Console.WriteLine("{0}到{1}之间所以整数的和为:{2}",a,b,sum);
}
}
}