翼度科技»论坛 编程开发 .net 查看内容

C#学习笔记---异常捕获和变量运算符

7

主题

7

帖子

21

积分

新手上路

Rank: 1

积分
21
异常捕获

使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生。
try{}catch{}finally{}结构
  1. //异常捕获
  2. try
  3. {
  4.     string str=Console.ReadLine();
  5.     int i=int.Parse(str);
  6.     Console.WriteLine("输入的字符串数值转为int的数值"+i);
  7. }catch
  8. {
  9.     Console.WriteLine("请输入合法数字");
  10. }finally
  11. {
  12.     //无论正常执行还是是否进行异常捕获 都会执行
  13.     Console.WriteLine("执行完毕");
  14. }
复制代码
运算符

算术运算符

算术运算符是英语数值类型变量运算的运算符,运算结果仍旧为数值。
赋值运算符:=
注意:赋值运算符理解将右边数值赋值给左边变量。
算术运算符:
  1. //算术数运算符
  2. //加+
  3. int i1=5;
  4. int sum=1+2+3+i1*2;
  5. //减 -
  6. int j1=6;
  7. int sub=15-j1-2;
  8. //乘 *
  9. int c=5;
  10. int c1=5*c;
  11. //除 /
  12. int d=28;
  13. int d1=d/4;
  14. //取模 (取余) %
  15. int e=12;
  16. e=e%5;
  17. Console.WriteLine(e);
复制代码
算术运算符的优先级
乘除取余 > 加减
  1. int a = 1 + 2 * 3 / 2 + 1 + 2 * 3; //=11
  2. Console.WriteLine(a);
  3. a = 1 + 4 % 2 * 3 / 2 + 1; //2
  4. Console.WriteLine(a);
  5. //括号可以改变优先级 优先计算括号内内容
  6. a = 1 + 4 % (2 * 3 / 2) + 1; //3
  7. Console.WriteLine(a);
  8. //多组括号 先算最里层括号 依次往外算
  9. a = 1 + (4 % (2 * (3 / 2))) + 1; //2
  10. Console.WriteLine(a);
复制代码
复合运算符
+=  -= *= /= %= 相当于对自身进行某项算术操作之后的结果重新赋给自己
  1. //符合运算符
  2. int i3 = 1;
  3. i3 = i3 + 2;
  4. Console.WriteLine(i3);
  5. i3 = 1;
  6. i3 += 2;//i3 = i3 + 2;
  7. Console.WriteLine(i3);
  8. i3 = 2;
  9. i3 += 2;//4
  10. i3 -= 2;//2
  11. i3 /= 2;//1
  12. i3 *= 2;//2
  13. i3 %= 2;//0
  14. Console.WriteLine(i3);
  15. int i4 = 10;
  16. // i4 += 4
  17. i4 += 20 * 2 / 10;
  18. Console.WriteLine(i4);
  19. //注意:复合运算符 只能进行一种运算 不能混合运算
  20. //i4 */-= 2;
复制代码
自增/减运算符
注意理解前置还是后置的运算符,区别先用后自增/减还是先自增/减再使用。
可以理解为电击小子打怪物,小光先变身成电击小子打怪兽还是打完怪兽再变身。
  1. //自增运算符
  2. //自增运算符  让自己+1
  3. a2 = 1;
  4. a2++;//先用再加
  5. Console.WriteLine(a2);
  6. ++a2;//先加再用
  7. Console.WriteLine(a2);
  8. a2 = 1;
  9. Console.WriteLine(a2++);//1
  10. //2
  11. Console.WriteLine(++a2);//3
  12. //自减运算符 让自己-1
  13. a2 = 1;
  14. a2--;//先用再减
  15. --a2;//先减再用
  16. a2 = 1;
  17. Console.WriteLine(a2--);//1
  18. //0
  19. Console.WriteLine(--a2);//-1
  20. //思考?这个
  21. int a = 10, b = 20;
  22. // 11 + 20
  23. int number1 = ++a + b;
  24. Console.WriteLine(number1);//31
  25. a = 10;
  26. b = 20;
  27. //10 + 20
  28. int number2 = a + b++;
  29. Console.WriteLine(number2);//30
  30. a = 10;
  31. b = 20;
  32. //10 + 21 + 11
  33. int number3 = a++ + ++b + a++;
  34. Console.WriteLine(number3);//42
  35. Console.WriteLine(a);//12
复制代码
字符串的拼接

  • 使用+拼接
    1. string str = "123";
    2. //用+号进行字符串拼接
    3. str = str + "456";
    4. Console.WriteLine(str);
    5. str = str + 1;
    6. Console.WriteLine(str);
    7. //使用+=
    8. str = "123";
    9. str += "1" + 4 + true;
    10. Console.WriteLine(str);
    11. //注意:只要遇到字符串,就是转为字符串
    12. string str = "畅知";
    13. str += 1 + 2 + 3 + 4;
    14. Console.WriteLine(str);//畅知10
    15. str = "";
    16. str += "" + 1 + 2 + 3 + 4;//开头就变为字符串
    17. Console.WriteLine(str);//1234
    18. str = "";
    19. str += 1 + 2 + "" + (3 + 4);//先算括号,从首到尾计算
    20. Console.WriteLine(str);//37
    21. str = "123";
    22. str = str + (1 + 2 + 3);
    23. Console.WriteLine(str);//1236
    复制代码
  • 使用占位符替换方式拼接(占位符从0开始,用{}括起来)
    1. string str2 = string.Format("我是{0}, 我今年{1}岁, 爱好:{2}", "畅知", 21, "我爱写博客!!");
    2. Console.WriteLine(str2);
    3. str2 = string.Format("asdf{0},{1},sdfasdf{2}", 1, true, false);
    4. Console.WriteLine(str2);
    复制代码
条件运算符

条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。
条件运算符的优先级要低于算术运算符
  1. //条件运算符
  2. int a = 5;
  3. int b = 10;
  4. //大于
  5. bool result = a > b;
  6. Console.WriteLine(result);
  7. //小于
  8. result = a < b;
  9. Console.WriteLine(result);
  10. //大于等于
  11. result = a >= b;
  12. Console.WriteLine(result);
  13. //小于等于
  14. result = a <= b;
  15. Console.WriteLine(result);
  16. //等于
  17. result = a == b;
  18. Console.WriteLine(result);
  19. //不等于
  20. result = a != b;
  21. Console.WriteLine(result);
  22. //也可以直接和数值比较
  23. result=10>5;
  24. //优先级
  25. // 先计算 再比较
  26. result = a + 3 > a - 2 + 3;// true
  27. result = 3 + 3 < 5 - 1;//false
  28. //不同类型之间的比较
  29. //不同数值类型之间 可以随意进行条件运算符比较
  30. int i = 5;
  31. float f = 1.2f;
  32. double d = 12.4;
  33. short s = 2;
  34. byte by = 20;
  35. uint ui = 666;
  36. //只要是数值 就能够进行条件运算符比较  比较大于小于等于等等
  37. int i = 5;
  38. float f = 1.2f;
  39. double d = 12.4;
  40. short s = 2;
  41. byte by = 20;
  42. uint ui = 666;
  43. bool result;
  44. //只要是数值 就能够进行条件运算符比较  比较大于小于等于等等
  45. result = i > f;//true
  46. Console.WriteLine(result);
  47. result = f < d;//true
  48. Console.WriteLine(result);
  49. result = i > by;//false
  50. Console.WriteLine(result);
  51. result = f > ui;//false
  52. Console.WriteLine(result);
  53. result = ui > d;//true
  54. Console.WriteLine(result);
  55. //特殊类型 char string bool 只能同类型进行 == 和 != 比较
  56. string str = "123";
  57. char c = 'A';
  58. bool bo = true;
  59. result = str == "234";//false
  60. result = str == "123";//true
  61. result = str != "123";//false
  62. result = c == 'B';//false
  63. //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
  64. //字符参与比较大小时候将自身作为ASCII码比较
  65. //还可以和字符类型进行大小比较
  66. result = c > 123;
  67. result = c > 'B';
  68. result = bo == true;//true;
复制代码
逻辑运算符

逻辑与 & 逻辑或 || 逻辑非 !
逻辑运算符优先级 < 条件运算符 算术运算
条件运算符均为双目运算符,返回结果为bool类型的,使用其运算结果来做某些情况的判断。
条件运算符的优先级要低于算术运算符
  1. //条件运算符
  2. int a = 5;
  3. int b = 10;
  4. //大于
  5. bool result = a > b;
  6. Console.WriteLine(result);
  7. //小于
  8. result = a < b;
  9. Console.WriteLine(result);
  10. //大于等于
  11. result = a >= b;
  12. Console.WriteLine(result);
  13. //小于等于
  14. result = a <= b;
  15. Console.WriteLine(result);
  16. //等于
  17. result = a == b;
  18. Console.WriteLine(result);
  19. //不等于
  20. result = a != b;
  21. Console.WriteLine(result);
  22. //也可以直接和数值比较
  23. result=10>5;
  24. //优先级
  25. // 先计算 再比较
  26. result = a + 3 > a - 2 + 3;// true
  27. result = 3 + 3 < 5 - 1;//false
  28. //不同类型之间的比较
  29. //不同数值类型之间 可以随意进行条件运算符比较
  30. int i = 5;
  31. float f = 1.2f;
  32. double d = 12.4;
  33. short s = 2;
  34. byte by = 20;
  35. uint ui = 666;
  36. //只要是数值 就能够进行条件运算符比较  比较大于小于等于等等
  37. int i = 5;
  38. float f = 1.2f;
  39. double d = 12.4;
  40. short s = 2;
  41. byte by = 20;
  42. uint ui = 666;
  43. bool result;
  44. //只要是数值 就能够进行条件运算符比较  比较大于小于等于等等
  45. result = i > f;//true
  46. Console.WriteLine(result);
  47. result = f < d;//true
  48. Console.WriteLine(result);
  49. result = i > by;//false
  50. Console.WriteLine(result);
  51. result = f > ui;//false
  52. Console.WriteLine(result);
  53. result = ui > d;//true
  54. Console.WriteLine(result);
  55. //特殊类型 char string bool 只能同类型进行 == 和 != 比较
  56. string str = "123";
  57. char c = 'A';
  58. bool bo = true;
  59. result = str == "234";//false
  60. result = str == "123";//true
  61. result = str != "123";//false
  62. result = c == 'B';//false
  63. //不仅可以和自己类型进行 == != 还可以和数值类型进行比较
  64. //字符参与比较大小时候将自身作为ASCII码比较
  65. //还可以和字符类型进行大小比较
  66. result = c > 123;
  67. result = c > 'B';
  68. result = bo == true;//true;
复制代码
逻辑运算符

逻辑与 & 逻辑或 || 逻辑非 !
逻辑运算符优先级 < 条件运算符 算术运算
逻辑运算符中: !(逻辑非)优先级最高   &&(逻辑与)优先级高于||(逻辑或)
  1. //逻辑运算符
  2. //逻辑与&& 并且
  3. //规则: 对两个bool值进行逻辑运算 有假则假 同真为真
  4. bool result = true && false;
  5. Console.WriteLine(result);
  6. result = true && true;
  7. Console.WriteLine(result);
  8. result = false && true;
  9. Console.WriteLine(result);
  10. //bool相关的类型 bool变量  条件运算符
  11. //逻辑运算符优先级 低于 条件运算符 算术运算
  12. // true && true
  13. result = 3 > 1 && 1 < 2;
  14. Console.WriteLine(result);
  15. int i = 3;
  16. // 1 < i < 5;
  17. // true && true
  18. result = i > 1 && i < 5;
  19. Console.WriteLine(result);
  20. //多个逻辑与 组合运用
  21. int i2 = 5;
  22. // true && false && true && true
  23. //在没有括号的情况下 从左到右 依次看即可
  24. //有括号 先看括号内
  25. result = i2 > 1 && i2 < 5 && i > 1 && i < 5;
  26. Console.WriteLine(result);
  27. //符号 || 或者
  28. //规则 对两个bool值进行逻辑运算 有真则真 同假为假
  29. result = true || false;
  30. Console.WriteLine(result);
  31. result = true || true;
  32. Console.WriteLine(result);
  33. result = false || true;
  34. Console.WriteLine(result);
  35. result = false || false;
  36. Console.WriteLine(result);
  37. // false || true
  38. result = 3 > 10 || 3 < 5;
  39. Console.WriteLine(result);//true
  40. int a = 5;
  41. int b = 11;
  42. // true || true || false
  43. result = a > 1 || b < 20 || a > 5;
  44. Console.WriteLine(result);
  45. // ? && ?
  46. // ? || ?
  47. // ? 可以是写死的bool变量 或者 bool值
  48. // 还可以是 条件运算符相关
  49. //----------逻辑非!
  50. //符号 !
  51. //规则 对一个bool值进行取反  真变假  假变真
  52. result = !true;
  53. Console.WriteLine(result);
  54. result = !false;
  55. Console.WriteLine(result);
  56. result = !!true;
  57. Console.WriteLine(result);
  58. //逻辑非的 优先级 较高
  59. result = !(3 > 2);
  60. Console.WriteLine(result);
  61. a = 5;
  62. result = !(a > 5);
  63. Console.WriteLine(result);
  64. //混合使用逻辑运算符的优先级问题
  65. // 规则  !(逻辑非)优先级最高   &&(逻辑与)优先级高于||(逻辑或)
  66. // 逻辑运算符优先级 低于 算数运算符 条件运算符(逻辑非除外)
  67. bool gameOver = false;
  68. int hp = 100;
  69. bool isDead = false;
  70. bool isMustOver = true;
  71. //false || false && true || true;
  72. //false || false || true;
  73. result = gameOver || hp < 0 && !isDead || isMustOver;
  74. Console.WriteLine(result);
复制代码
逻辑运算符的短路原则(聪明的运算符)
  1. //短路原则
  2. //|| 判断原则为有真则真 所以第一个条件判断为真,则直接可以得出运算结果为真 便不会检查第二个
  3. //个运算条件的真假
  4. //同理,&& 若左边条件为假,则不会判断右边条件,直接可以得出运算结果为假
  5. //逻辑或 有真则真 那左边只要为真了 右边就不重要
  6. int i3=1;
  7. bool result = i3 > 0 || ++i3 >= 1;
  8. Console.WriteLine(i3);//1
  9. Console.WriteLine(result);
  10. // false && i3 ++ > 1;抛弃后面不去计算
  11. //逻辑与 有假则假 那左边只要为假了 右边就不重要
  12. result = i3 < 0 && i3++ > 1;
  13. Console.WriteLine(i3);//1
  14. Console.WriteLine(result);
  15. //思考?
  16. //求打印结果是什么?
  17. //注意运算符的优先级
  18. bool gameOver;
  19. bool isWin;
  20. int health = 100;
  21. gameOver = true;
  22. isWin = false;
  23. // true || false && true
  24. Console.Write(gameOver || isWin && health > 0);
复制代码
位运算符

位运算是基于二进制编码的运算,首先将值转换为二进制数值,然后对于位进行操作运算。
运算符:位与& 位或| 异或^ 位或 | 位取反! 左移>
  1. //位运算符
  2. //位与& 有0则0 全1才1
  3. // 对位运算 有0则0
  4. int a = 1;// 001
  5. int b = 5;// 101
  6. //  001
  7. //& 101
  8. //  001  =  1
  9. int c = a & b;
  10. Console.WriteLine(c);
  11. //多个数值进行位运算 没有括号时 从左到右 依次计算
  12. a = 1;//   001
  13. b = 5;//   101
  14. c = 19;//10011
  15. //  00001
  16. //& 00101
  17. //  00001
  18. //& 10011
  19. //  00001
  20. int d = a & b & c;
  21. Console.WriteLine(d);
  22. //位或| 有1则1,全0则0
  23. a = 1;//001
  24. b = 3;//011
  25. c = a | b;
  26. //  001
  27. //| 011
  28. //  011
  29. Console.WriteLine(c);
  30. a = 5; //  101
  31. b = 10;// 1010
  32. c = 20;//10100
  33. //  00101
  34. //| 01010
  35. //  01111
  36. //| 10100
  37. //  11111 => 1 + 2 + 4 + 8 + 16  =31
  38. Console.WriteLine(a | b | c);
  39. //异或^ =======
  40. //不同为1 相同为0
  41. // 对位运算 相同为0 不同为1
  42. a = 1; //001
  43. b = 5; //101
  44. // 001
  45. //^101
  46. // 100
  47. c = a ^ b;
  48. Console.WriteLine(c);
  49. a = 10; // 1010
  50. b = 11; // 1011
  51. c = 4;  //  100
  52. //  1010
  53. //^ 1011
  54. //  0001
  55. //^ 0100
  56. //  0101  = 5
  57. Console.WriteLine(a ^ b ^ c);
  58. //位取反 ~ 取反
  59. // 对位运算 0变1 1变0
  60. a = 5;
  61. // 0000 0000 0000 0000 0000 0000 0000 0101
  62. // 1111 1111 1111 1111 1111 1111 1111 1010
  63. // 反码补码知识  
  64. // 计算机中的二进制是以补码形式存储的
  65. //补码:正数的补码是本身  负数的补码是绝对值取反加一
  66. c = ~a;
  67. Console.WriteLine(c);
  68. //左移 右移
  69. // 规则 让一个数的2进制数进行左移和右移
  70. // 左移几位 右侧加几个0
  71. a = 5; // 101
  72. c = a << 5;
  73. // 1位 1010
  74. // 2位 10100
  75. // 3位 101000
  76. // 4位 1010000
  77. // 5位 10100000 = 32 + 128 = 160
  78. Console.WriteLine(c);
  79. // 右移几位 右侧去掉几个数
  80. a = 5; // 101
  81. c = a >> 2;
  82. // 1位 10
  83. // 2位 1
  84. Console.WriteLine(c);
  85. //练习----
  86. //99 ^ 33 和 76 | 85 的结果为?
  87. // 1100011 ^ 100001
  88. // 1100011
  89. //^0100001
  90. // 1000010
  91. Console.WriteLine(99 ^ 33);
  92. // 1001100 | 1010101
  93. // 1001100
  94. //|1010101
  95. // 1011101 => 64 + 29 = 93
  96. Console.WriteLine(76 | 85);
复制代码
三目运算符

  条件?A :B 条件为真则走A逻辑否则走B
  1. //三目运算符
  2. int a = 5;
  3. str = a < 1 ? "a大于1" : "a不满条件";
  4. Console.WriteLine(str);
  5. int i = a > 1 ? 123 : 234;
  6. //第一个空位 始终是结果为bool类型的表达式 bool变量 条件表达式 逻辑运算符表达式
  7. //第二三个空位 什么表达式都可以 只要保证他们的结果类型是一致的
  8. bool b = a > 1 ? a > 6 : !false;
复制代码
======
我会每天更新C#学习笔记,感兴趣的可以给个订阅!
点个订阅,练习C#代码不迷路!

来源:https://www.cnblogs.com/TonyCode/archive/2023/10/09/17751761.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具