C#整理7——函数


数据类型--变量常量--运算符表达式--语句(顺序,分支,循环)--数组--函数

函数

1、定义:能够独立完成某个功能的模块。
2、好处:1.结构更清析(编写、维护方便 )。2.代码重用。3.分工开发。
3、四要素:名称,输入(参数),输出(返回的类型),加工(函数体)

语法:
返回类型 函数名(参数类型 参数名,....)
{
函数体
}

 

函数调用:
[数据类型 变量名 = ]函数(参数);

函数调用时:调用的参数和函数定义的参数保持一对待:个数,类型,对应。

形参:形式参数。——函数定义的参数。
实参:实际参数。——函数调用的参数。

实参、形参传值的规律——传值,传址。

传值:参于整型、浮点、bool、char这几种内建类型在函数传递参数的时候,默认都是传值。
传值是把实参做一个副本(copy)传递给形参。
m = 30;
Add(m);
static void Add(int a)
{
a += 20;
}
传址:默认情况下,数组就是传地址,字符串也是传地址。
对于内建的整型、浮点、bool、char这些类型,如果要变成传址的话,需要在前面加ref
m = 30;
Add(ref m);
static void Add(ref int a)
{
a += 20;
}

 

对于传值和传址大家要记住 :
1.什么是传值,什么是传址?这个要分清楚。
2.默认情况下,哪些类型是传值?哪些类型是传址?
3.对于默认传值的类型,如何让他们变为传址?ref

以后为了防止因为传值,传址引起来的错误 ,建议大家采用返回值的形式,明确返回的数据

递归——仅做了解。
函数自己调自己。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Class7
 9     {
10         static void Main(string[] args)
11         {
12             Test(0);
13         }
14         static void Test(int a)
15         {
16             //if条件的return很重要,没有的话就永远出不来了。
17             if (a > 5)
18             {
19                 return;
20             }
21             a++;
22             Console.WriteLine("正在做第" + a + "个梦");
23             Test(a);
24             Console.WriteLine("" + a + "个梦醒了");
25         }
26     }
27 }
View Code

 

 

常用的类:
(一)数学类:Math
1.Math.Ceiling(小数/整数):返回大于当前小数的最小整数。——天花板数
2.Math.Floor(小数/整数):返回小于当前小数的最大整数。——地板数
Console.WriteLine(Math.Ceiling(3.14)); //4
Console.WriteLine(Math.Floor(3.14)); //3
Console.WriteLine(Math.Ceiling(3.0)); //3

3.Math.Pow(2,3)求指数。相当于2的3次方
4.Math.Sqrt(16)开平方。
5.四舍五入。
Math.Round(3.63); //4
Math.Round(3.14); //3

(二)日期时间:DateTime
构造:DateTime dt = new DateTime([1990,2,5[,3,44,21]]);
DateTime dt = new DateTime(); //?
DateTime dt = new DateTime(1990, 2, 5);//?
DateTime dt = new DateTime(1990, 2, 5, 3, 44, 25);//?
当前时间:
DateTime dt = DateTime.Now;

日期时间对象的数据:
Year,Month,Day,Hour,Minite,Second,MilliSecond
DayOfWeek——星期几。DayOfYear——一年中的第几天。
Date——取期日期部份。TimeOfDay——取期时间部份。
日期时间对象的函数:
AddYears(int num)
AddMonths(int num)
AddDays(int num)
AddHours(int num)
AddMinutes(int num)
AddSeconds(int num)

日期时间型数据可以直接相减,返回两个日期之间差的天数和时间。

ToString(格式字符串)函数:把日按照某种格式显示出来。
格式字符串:
yyyy——四位数字的年份
yy——两位数字的年份
MM——两位数字的月分,不足两位添0
M——1-2位数字的月份
dd——两位数字的天,不足两位添0
d——1-2位数字的天。
hh——
h——
mm——
m——
ss——
s——
ms——毫秒。
例如:
DateTime dt = DateTime.Now;
Console.WriteLine(dt.ToString("yyyy年MM月dd日hh时mm分ss秒"));

不止是日期时间型数据的ToString()函数中可以放格式化字符中。整数,小数的ToString()中也可以放格式化字符串。
小数和整数类型的格式化符号主要是有四个。
.——小数点
,——整数部份三位的分隔符
#——任意位数字,有几位显示几位
0——至少一位数字,不足则补0.

例:
#.00——必须保留两位小数。


(三)字符串
*Length:字符串的长度。

ToLower():全都转成小写
ToUpper():全都转成大写

TrimStart():
TrimEnd():
Trim():压两头的空格。

*StartsWidth("字符串"):(bool)是否以括中的字符串开头,是--返回true。
*EndsWidth("字符串"):(bool)是否以括号中的字符串结尾,是--返回true。
*Contains("字符串"):(bool)是否包括括号中的字符串。是--返回true。

*IndexOf("子串"):(int)返回子串在字符串中第一次出现的位置。
*LastIndexOf("子串"):(int)返回子串在字符串中最后一次出现的位置。
以上两函数,如果在字符串中找不到相应的子串,返回-1

*Substring(int start[,int length]):(string)截取子串。
Replace(string old,string new):(string)把字符串的old串换成new串
*Split('字符'):(string[])按照括号中的字符把字符串拆开成数组中的元素。

 

1.从键盘输入一个正确的身份证号,获取其生日。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.Write("请输入一个身份证号");
13             string s = Convert.ToString(Console.ReadLine());
14             Console.Write("生日是" + s.Substring(6, 8)); 
15         }
16     }
17 }
View Code

 


2.从路径:C:\Users\Administrator\Desktop\1220\0104\aaa.txt中,获取文件名。(用两种法做)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = "C:\\Users\\Administrator\\Desktop\\1220\\0104\\aaa.txt";
13             string[] ss = s.Split('\\');
14             for (int i = 0; i < ss.Length; i++)
15             {
16                 Console.WriteLine(ss[i]);
17             }
18         }
19     }
20 }
View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             string s = "C:\\Users\\Administrator\\Desktop\\1220\\0104\\aaa.txt";
13             int ss = s.LastIndexOf("\\");
14             Console.Write("文件名是:" + s.Substring(ss + 1));
15         }
16     }
17 }
View Code

 


3.用户从键盘上输入一个邮箱。验证是否正确。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace ConsoleApplication1
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             Console.Write("请输入一个邮箱:");
13             string s = Convert.ToString(Console.ReadLine());
14             int a = s.IndexOf(".");
15             int a1 = s.LastIndexOf(".");
16             int b = s.IndexOf("@");
17             int b1 = s.LastIndexOf("@");
18             if (a == a1)
19             {
20                 if (a == 0 || a == s.Length - 1 || a1 == 0 || a1 == s.Length - 1 || b == 0 || b == s.Length - 1 || b != b1 || a - b <= 1 || a1 - b <= 1)
21                 {
22 
23                     Console.Write("你输入的邮箱错误");
24                     return;
25                 }
26             }
27             else if (a1 - a != 4 || a == 0 || a == s.Length - 1 || a1 == 0 || a1 == s.Length - 1 || b == 0 || b == s.Length - 1 || b != b1 || a - b <= 1 || a1 - b <= 1)
28             {
29                 Console.Write("你输入的邮箱错误");
30                 return;
31             }
32             Console.Write("您输入的邮箱正确");
33         }
34     }
35 }
View Code

 

转载于:https://www.cnblogs.com/viven/p/4323400.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值