总结:学习了基础关键词的作用 和一些算法
命名规范
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| int myName = 12; int yourName = 13; string myString = "我的字符串";
class MyArray{
}
int MyFunction (){
}
#region
#endregion
|
变量类型
1字节=8比特=8位 例:1001 0100
无符号 正整数集 ulong (64位) uint (32位) ushort (16位) byte (8位)
有符号 整数集 long (64位) int (32位) short (16位) sbyte (8位)
浮点数 小数 decimal (128位) double (64位) float (32位)
特殊字符 字符串 字符 string (无位数限制) char (16位)bool (8位)
声明常量 变量前加“const” 关键字
const float PI = 3.14159265f;
转义字符
1 2 3 4 5
|
Console.WriteLine("\"哈哈哈\""); Console.WriteLine(@"哈\哈哈");
|
数据类型转换
隐式转换
小范围可以隐式转换大范围
1 2 3
| long i =1; int i2 =1; i=i2;
|
char可以隐式转换成整数和浮点,转换结果为Ascii码。
显式转换
括号强转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
int i = 1; short s = 1; s = (short)i;
byte b = 1; uint ui = 1; b = (byte)ui;
float f = 18.789f; i = (int)f; Console.WriteLine(i);
|
Parse法
字符串转对应类型
1 2 3 4 5 6 7 8 9
| string str1 = "123"; int i2 = int.Parse(str1); i2 += 100; Console.WriteLine(i2);
short s3 = (short)int.Parse("4000000"); Console.WriteLine(s3);
|
Convert法
适用性强 什么都能转
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int a = Convert.ToInt32("12"); string str2 = Convert.ToString(321);
a = Convert.ToInt32(3.6789f); Console.WriteLine(a);
float f1 = Convert.ToSingle("13.567");
Console.WriteLine(a);
|
其他类型转String
拼接打印
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
string str3 = 9888.ToString(); Console.WriteLine(str3);
int a4 = 1 ; str3 = a4.ToString(); bool b1 = true; str3 = b1.ToString(); Console.WriteLine(str3);
Console.WriteLine("数字是:" + a4 + b1);
Console.WriteLine("数字是:" + a4.ToString() + b1.ToString());
|
异常捕获
1 2 3 4 5 6 7 8 9
| try { string str = Console.ReadLine(); int.Parse(str); } catch { Console.WriteLine("请输入合法数字"); }
|
自增运算符
逻辑运算符的短路规则
与:&& 有假则假 同真为真
或:|| 有真为真 同假为假
非:! 反转真假
运算优先级: ! > && > ||
i3打印结果为1
位运算符
三目运算符
可以实现简单的逻辑运算 if和else
分支语句
If
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| if (bool值) { 如果为真则执行此语句块的代码 }
if (bool值) { 如果为真则执行此语句块的代码 }
else (bool值) { 否则执行此段语句块代码 }
if (bool值) { 如果为真则执行此语句块的代码 } else if (bool值) { 否则执行此段语句块代码 } else if (bool值) { 否则执行此段语句块代码 }
else (bool值) { 否则执行此段语句块代码 }
if (bool值) { if(bool值) { } else if (bool值) { }
} else if (bool值) {
}
if(!bool值) { 如果为真则执行此语句块的代码 }
|
Switch
用于枚举
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| switch (变量) { case 常量: 满足条件所执行的代码 break; case 常量: 满足条件所执行的代码 break; case 常量: 满足条件所执行的代码 break; default: 如果上面的条件都不满足 就会执行default break; }
switch (变量) { case 常量1: case 常量2: 满足条件所执行的代码 break; default: 如果上面的条件都不满足 就会执行default break;
int a = 0; switch { case 1: Console.WriteLine("a为1") break; case 2: Console.WriteLine("a为2") break; case 3: Console.WriteLine("a为3") break; default: Console.WriteLine("a不满足条件") break; }
|
循环语句
For循环
使用较多
1 2 3 4 5 6 7 8 9 10 11 12
| for(int i =0; i<10; ++i) { Console.WriteLine(i); }
for(;;) { Console.WriteLine("你好"); }
|
While循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| while(bool值) { 满足条件时就会执行该语句块 执行玩会回到while开头再次判断 }
int i = 0 ; while(i<10) { ++i; }
|
do while循环
用的比较少
1 2 3 4 5
| do { 判断条件在后面 所以先执行一次本语句块代码再做判断
}while(bool值);
|
break:跳出循环
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| while(true) { Console.WriteLine("break之前的代码"); break; Console.WriteLine("break之后的代码 不会被执行"); } Console.WriteLine("循环外的代码");
int i = 0 ; while(true) { ++i; if(i==10) { break; } }
|
continue:回到循环开始
1 2 3 4 5 6 7 8
| int i = 0 ; while(true) { Console.WriteLine("continue之前的代码"); continue; Console.WriteLine("continue之后的代码不会执行"); } Console.WriteLine("循环外的代码不会执行");
|
补充内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| char c = Console.ReadKey().KeyChar;
char c = Console.ReadKey(true).KeyChar;
Console.Clear();
Console.SetCursorPosition(10,5);
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.White; Console.Clear();
Console.CursorVisible = false;
Environment.Exit(0);
|