函数 有四种格式
格式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();
}