异常捕获
使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生。
try{}catch{}finally{}结构- //异常捕获
- try
- {
- string str=Console.ReadLine();
- int i=int.Parse(str);
- Console.WriteLine("输入的字符串数值转为int的数值"+i);
- }catch
- {
- Console.WriteLine("请输入合法数字");
- }finally
- {
- //无论正常执行还是是否进行异常捕获 都会执行
- Console.WriteLine("执行完毕");
- }
复制代码 运算符
算术运算符
算术运算符是英语数值类型变量运算的运算符,运算结果仍旧为数值。
赋值运算符:=
注意:赋值运算符理解将右边数值赋值给左边变量。
算术运算符:- //算术数运算符
- //加+
- int i1=5;
- int sum=1+2+3+i1*2;
- //减 -
- int j1=6;
- int sub=15-j1-2;
- //乘 *
- int c=5;
- int c1=5*c;
- //除 /
- int d=28;
- int d1=d/4;
- //取模 (取余) %
- int e=12;
- e=e%5;
- Console.WriteLine(e);
复制代码 算术运算符的优先级
乘除取余 > 加减- int a = 1 + 2 * 3 / 2 + 1 + 2 * 3; //=11
- Console.WriteLine(a);
- a = 1 + 4 % 2 * 3 / 2 + 1; //2
- Console.WriteLine(a);
- //括号可以改变优先级 优先计算括号内内容
- a = 1 + 4 % (2 * 3 / 2) + 1; //3
- Console.WriteLine(a);
- //多组括号 先算最里层括号 依次往外算
- a = 1 + (4 % (2 * (3 / 2))) + 1; //2
- Console.WriteLine(a);
复制代码 复合运算符
+= -= *= /= %= 相当于对自身进行某项算术操作之后的结果重新赋给自己- //符合运算符
- int i3 = 1;
- i3 = i3 + 2;
- Console.WriteLine(i3);
- i3 = 1;
- i3 += 2;//i3 = i3 + 2;
- Console.WriteLine(i3);
- i3 = 2;
- i3 += 2;//4
- i3 -= 2;//2
- i3 /= 2;//1
- i3 *= 2;//2
- i3 %= 2;//0
- Console.WriteLine(i3);
- int i4 = 10;
- // i4 += 4
- i4 += 20 * 2 / 10;
- Console.WriteLine(i4);
- //注意:复合运算符 只能进行一种运算 不能混合运算
- //i4 */-= 2;
复制代码 自增/减运算符
注意理解前置还是后置的运算符,区别先用后自增/减还是先自增/减再使用。
可以理解为电击小子打怪物,小光先变身成电击小子打怪兽还是打完怪兽再变身。- //自增运算符
- //自增运算符 让自己+1
- a2 = 1;
- a2++;//先用再加
- Console.WriteLine(a2);
- ++a2;//先加再用
- Console.WriteLine(a2);
- a2 = 1;
- Console.WriteLine(a2++);//1
- //2
- Console.WriteLine(++a2);//3
- //自减运算符 让自己-1
- a2 = 1;
- a2--;//先用再减
- --a2;//先减再用
- a2 = 1;
- Console.WriteLine(a2--);//1
- //0
- Console.WriteLine(--a2);//-1
- //思考?这个
- int a = 10, b = 20;
- // 11 + 20
- int number1 = ++a + b;
- Console.WriteLine(number1);//31
- a = 10;
- b = 20;
- //10 + 20
- int number2 = a + b++;
- Console.WriteLine(number2);//30
- a = 10;
- b = 20;
- //10 + 21 + 11
- int number3 = a++ + ++b + a++;
- Console.WriteLine(number3);//42
- Console.WriteLine(a);//12
复制代码 字符串的拼接
- 使用+拼接
- string str = "123";
- //用+号进行字符串拼接
- str = str + "456";
- Console.WriteLine(str);
- str = str + 1;
- Console.WriteLine(str);
- //使用+=
- str = "123";
- str += "1" + 4 + true;
- Console.WriteLine(str);
- //注意:只要遇到字符串,就是转为字符串
- string str = "畅知";
- str += 1 + 2 + 3 + 4;
- Console.WriteLine(str);//畅知10
- str = "";
- str += "" + 1 + 2 + 3 + 4;//开头就变为字符串
- Console.WriteLine(str);//1234
- str = "";
- str += 1 + 2 + "" + (3 + 4);//先算括号,从首到尾计算
- Console.WriteLine(str);//37
- str = "123";
- str = str + (1 + 2 + 3);
- Console.WriteLine(str);//1236
复制代码 - 使用占位符替换方式拼接(占位符从0开始,用{}括起来)
- string str2 = string.Format("我是{0}, 我今年{1}岁, 爱好:{2}", "畅知", 21, "我爱写博客!!");
- Console.WriteLine(str2);
- str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false);
- Console.WriteLine(str2);
复制代码 条件运算符
条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。
条件运算符的优先级要低于算术运算符- //条件运算符
- int a = 5;
- int b = 10;
- //大于
- bool result = a > b;
- Console.WriteLine(result);
- //小于
- result = a < b;
- Console.WriteLine(result);
- //大于等于
- result = a >= b;
- Console.WriteLine(result);
- //小于等于
- result = a <= b;
- Console.WriteLine(result);
- //等于
- result = a == b;
- Console.WriteLine(result);
- //不等于
- result = a != b;
- Console.WriteLine(result);
- //也可以直接和数值比较
- result=10>5;
- //优先级
- // 先计算 再比较
- result = a + 3 > a - 2 + 3;// true
- result = 3 + 3 < 5 - 1;//false
- //不同类型之间的比较
- //不同数值类型之间 可以随意进行条件运算符比较
- int i = 5;
- float f = 1.2f;
- double d = 12.4;
- short s = 2;
- byte by = 20;
- uint ui = 666;
- //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
- int i = 5;
- float f = 1.2f;
- double d = 12.4;
- short s = 2;
- byte by = 20;
- uint ui = 666;
- bool result;
- //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
- result = i > f;//true
- Console.WriteLine(result);
- result = f < d;//true
- Console.WriteLine(result);
- result = i > by;//false
- Console.WriteLine(result);
- result = f > ui;//false
- Console.WriteLine(result);
- result = ui > d;//true
- Console.WriteLine(result);
- //特殊类型 char string bool 只能同类型进行 == 和 != 比较
- string str = "123";
- char c = 'A';
- bool bo = true;
- result = str == "234";//false
- result = str == "123";//true
- result = str != "123";//false
- result = c == 'B';//false
- //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
- //字符参与比较大小时候将自身作为ASCII码比较
- //还可以和字符类型进行大小比较
- result = c > 123;
- result = c > 'B';
- result = bo == true;//true;
复制代码 逻辑运算符
逻辑与 & 逻辑或 || 逻辑非 !
逻辑运算符优先级 < 条件运算符 算术运算
条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。
条件运算符的优先级要低于算术运算符- //条件运算符
- int a = 5;
- int b = 10;
- //大于
- bool result = a > b;
- Console.WriteLine(result);
- //小于
- result = a < b;
- Console.WriteLine(result);
- //大于等于
- result = a >= b;
- Console.WriteLine(result);
- //小于等于
- result = a <= b;
- Console.WriteLine(result);
- //等于
- result = a == b;
- Console.WriteLine(result);
- //不等于
- result = a != b;
- Console.WriteLine(result);
- //也可以直接和数值比较
- result=10>5;
- //优先级
- // 先计算 再比较
- result = a + 3 > a - 2 + 3;// true
- result = 3 + 3 < 5 - 1;//false
- //不同类型之间的比较
- //不同数值类型之间 可以随意进行条件运算符比较
- int i = 5;
- float f = 1.2f;
- double d = 12.4;
- short s = 2;
- byte by = 20;
- uint ui = 666;
- //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
- int i = 5;
- float f = 1.2f;
- double d = 12.4;
- short s = 2;
- byte by = 20;
- uint ui = 666;
- bool result;
- //只要是数值 就能够进行条件运算符比较 比较大于小于等于等等
- result = i > f;//true
- Console.WriteLine(result);
- result = f < d;//true
- Console.WriteLine(result);
- result = i > by;//false
- Console.WriteLine(result);
- result = f > ui;//false
- Console.WriteLine(result);
- result = ui > d;//true
- Console.WriteLine(result);
- //特殊类型 char string bool 只能同类型进行 == 和 != 比较
- string str = "123";
- char c = 'A';
- bool bo = true;
- result = str == "234";//false
- result = str == "123";//true
- result = str != "123";//false
- result = c == 'B';//false
- //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
- //字符参与比较大小时候将自身作为ASCII码比较
- //还可以和字符类型进行大小比较
- result = c > 123;
- result = c > 'B';
- result = bo == true;//true;
复制代码 逻辑运算符
逻辑与 & 逻辑或 || 逻辑非 !
逻辑运算符优先级 < 条件运算符 算术运算
逻辑运算符中: !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)- //逻辑运算符
- //逻辑与&& 并且
- //规则: 对两个bool值进行逻辑运算 有假则假 同真为真
- bool result = true && false;
- Console.WriteLine(result);
- result = true && true;
- Console.WriteLine(result);
- result = false && true;
- Console.WriteLine(result);
- //bool相关的类型 bool变量 条件运算符
- //逻辑运算符优先级 低于 条件运算符 算术运算
- // true && true
- result = 3 > 1 && 1 < 2;
- Console.WriteLine(result);
- int i = 3;
- // 1 < i < 5;
- // true && true
- result = i > 1 && i < 5;
- Console.WriteLine(result);
- //多个逻辑与 组合运用
- int i2 = 5;
- // true && false && true && true
- //在没有括号的情况下 从左到右 依次看即可
- //有括号 先看括号内
- result = i2 > 1 && i2 < 5 && i > 1 && i < 5;
- Console.WriteLine(result);
- //符号 || 或者
- //规则 对两个bool值进行逻辑运算 有真则真 同假为假
- result = true || false;
- Console.WriteLine(result);
- result = true || true;
- Console.WriteLine(result);
- result = false || true;
- Console.WriteLine(result);
- result = false || false;
- Console.WriteLine(result);
- // false || true
- result = 3 > 10 || 3 < 5;
- Console.WriteLine(result);//true
- int a = 5;
- int b = 11;
- // true || true || false
- result = a > 1 || b < 20 || a > 5;
- Console.WriteLine(result);
- // ? && ?
- // ? || ?
- // ? 可以是写死的bool变量 或者 bool值
- // 还可以是 条件运算符相关
- //----------逻辑非!
- //符号 !
- //规则 对一个bool值进行取反 真变假 假变真
- result = !true;
- Console.WriteLine(result);
- result = !false;
- Console.WriteLine(result);
- result = !!true;
- Console.WriteLine(result);
- //逻辑非的 优先级 较高
- result = !(3 > 2);
- Console.WriteLine(result);
- a = 5;
- result = !(a > 5);
- Console.WriteLine(result);
- //混合使用逻辑运算符的优先级问题
- // 规则 !(逻辑非)优先级最高 &&(逻辑与)优先级高于||(逻辑或)
- // 逻辑运算符优先级 低于 算数运算符 条件运算符(逻辑非除外)
- bool gameOver = false;
- int hp = 100;
- bool isDead = false;
- bool isMustOver = true;
- //false || false && true || true;
- //false || false || true;
- result = gameOver || hp < 0 && !isDead || isMustOver;
- Console.WriteLine(result);
复制代码 逻辑运算符的短路原则(聪明的运算符)- //短路原则
- //|| 判断原则为有真则真 所以第一个条件判断为真,则直接可以得出运算结果为真 便不会检查第二个
- //个运算条件的真假
- //同理,&& 若左边条件为假,则不会判断右边条件,直接可以得出运算结果为假
- //逻辑或 有真则真 那左边只要为真了 右边就不重要
- int i3=1;
- bool result = i3 > 0 || ++i3 >= 1;
- Console.WriteLine(i3);//1
- Console.WriteLine(result);
- // false && i3 ++ > 1;抛弃后面不去计算
- //逻辑与 有假则假 那左边只要为假了 右边就不重要
- result = i3 < 0 && i3++ > 1;
- Console.WriteLine(i3);//1
- Console.WriteLine(result);
- //思考?
- //求打印结果是什么?
- //注意运算符的优先级
- bool gameOver;
- bool isWin;
- int health = 100;
- gameOver = true;
- isWin = false;
- // true || false && true
- Console.Write(gameOver || isWin && health > 0);
复制代码 位运算符
位运算是基于二进制编码的运算,首先将值转换为二进制数值,然后对于位进行操作运算。
运算符:位与& 位或| 异或^ 位或 | 位取反! 左移>三目运算符
条件?A :B 条件为真则走A逻辑否则走B- //三目运算符
- int a = 5;
- str = a < 1 ? "a大于1" : "a不满条件";
- Console.WriteLine(str);
- int i = a > 1 ? 123 : 234;
- //第一个空位 始终是结果为bool类型的表达式 bool变量 条件表达式 逻辑运算符表达式
- //第二三个空位 什么表达式都可以 只要保证他们的结果类型是一致的
- bool b = a > 1 ? a > 6 : !false;
复制代码 ======
我会每天更新C#学习笔记,感兴趣的可以给个订阅!
点个订阅,练习C#代码不迷路!
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |