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

这篇具有很好参考价值的文章主要介绍了C#学习笔记---异常捕获和变量运算符。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

异常捕获

使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生。

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

字符串的拼接

  1. 使用+拼接

    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
    
  2. 使用占位符替换方式拼接(占位符从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);

位运算符

位运算是基于二进制编码的运算,首先将值转换为二进制数值,然后对于位进行操作运算。
运算符:位与& 位或| 异或^ 位或 | 位取反! 左移<< 右移>>

//位运算符
//位与& 有0则0 全1才1
// 对位运算 有0则0
int a = 1;// 001
int b = 5;// 101
//  001
//& 101
//  001  =  1
int c = a & b;
Console.WriteLine(c);

//多个数值进行位运算 没有括号时 从左到右 依次计算
a = 1;//   001
b = 5;//   101
c = 19;//10011
//  00001
//& 00101
//  00001
//& 10011
//  00001
int d = a & b & c;
Console.WriteLine(d);

//位或| 有1则1,全0则0
 a = 1;//001
b = 3;//011
c = a | b;
//  001
//| 011
//  011
Console.WriteLine(c);

a = 5; //  101
b = 10;// 1010
c = 20;//10100
//  00101
//| 01010
//  01111
//| 10100
//  11111 => 1 + 2 + 4 + 8 + 16  =31

Console.WriteLine(a | b | c);

//异或^ =======
//不同为1 相同为0
// 对位运算 相同为0 不同为1
a = 1; //001
b = 5; //101
// 001
//^101
// 100
c = a ^ b;
Console.WriteLine(c);

a = 10; // 1010
b = 11; // 1011
c = 4;  //  100
//  1010
//^ 1011
//  0001
//^ 0100
//  0101  = 5
Console.WriteLine(a ^ b ^ c);

//位取反 ~ 取反
// 对位运算 0变1 1变0
a = 5; 
// 0000 0000 0000 0000 0000 0000 0000 0101
// 1111 1111 1111 1111 1111 1111 1111 1010
// 反码补码知识  
// 计算机中的二进制是以补码形式存储的
//补码:正数的补码是本身  负数的补码是绝对值取反加一
c = ~a;
Console.WriteLine(c);

//左移 右移
// 规则 让一个数的2进制数进行左移和右移
// 左移几位 右侧加几个0
a = 5; // 101
c = a << 5;
// 1位 1010
// 2位 10100
// 3位 101000
// 4位 1010000
// 5位 10100000 = 32 + 128 = 160
Console.WriteLine(c);

// 右移几位 右侧去掉几个数
a = 5; // 101
c = a >> 2;
// 1位 10
// 2位 1
Console.WriteLine(c);
//练习----
 //99 ^ 33 和 76 | 85 的结果为?

// 1100011 ^ 100001
// 1100011
//^0100001
// 1000010
Console.WriteLine(99 ^ 33);

// 1001100 | 1010101
// 1001100
//|1010101
// 1011101 => 64 + 29 = 93
Console.WriteLine(76 | 85);

三目运算符

条件?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#代码不迷路!文章来源地址https://www.toymoban.com/news/detail-711688.html

到了这里,关于C#学习笔记---异常捕获和变量运算符的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • JAVA学习笔记——第四章 运算符

    🔥 博客主页 : A_SHOWY 🎥 系列专栏 :力扣刷题总结录 数据结构  云计算  数字图像处理  力扣每日一题_ 运算符是一种特殊的符号,用于表示数据的运算、赋值和比较 取模 %的本质: a - (int)a / b * b//当a是小数时 自增 独立语句使用时,++i和i++没有区别的。但是如果作

    2024年01月20日
    浏览(47)
  • Lingo入门学习笔记(1)——函数与运算符

    本文记录了学习b站浩然玩转高考物理up主的lingo速成视频进行学习笔记 @abs(x):绝对值函数,返回x的绝对值; @cos(x):余弦函数,返回x的余弦值(x的单位是弧度); @exp(x):指数函数,返回数学公式: $e^{x} $的值(其中e为自然对数的底); @floor(x):取整函数,返回x的整数部分(向最

    2024年02月07日
    浏览(23)
  • 第02章_变量与运算符(关键字,标识符,变量,基本数据类型,进制,运算符,字符集)

    定义: 被Java语言赋予了特殊含义,用做专门用途的字符串(或单词) HelloWorld案例中,出现的有 class 、 public 、 static 、 void 等,这些单词已经被Java定义好了。 特点:全部都是 小写字母 。 比较多,不需要死记硬背,学到哪里记到哪里即可。 官方地址:

    2024年01月22日
    浏览(45)
  • # 实验二 Python运算符与变量

    1.了解和掌握Python的运算符与变量 2.熟练运用python的运算符 1、写出判断一个数是否能同时被2和5整除的条件语句, 并且打印对应的结果。 x = if x % 2 == 0 and x % 5 == 0: ​print(“x能够被xxxxx”) else: ​print() 2、写出判断一个数是否能够被2或者5整除,但是不能同时被2或者5整除的条件

    2024年02月16日
    浏览(32)
  • Java数据类型,变量与运算符

    常量是在程序运行期间,固定不变的量称为常量。 在以上程序中,输出的Hello Word,其中的“Hello Word”就是字面常量。 字面常量的分类: 字符串常量 整形常量 浮点数常量 字符常量 布尔常量 空常量 注意:字符串,整形,浮点型,字符型以及布尔型,在Java中都称为数据类型

    2024年02月08日
    浏览(43)
  • 初识Java:数据类型与变量、运算符

    哈喽大家好,这篇文章我将为大家分享关于Java的数据类型与变量和运算符。 在Java中数据类型分为基本数据类型与引用数据类型,今天我们着重讲解基本数据类型。 基本数据类型又分为整型、浮点型、字符型以及布尔类型,我们来看下面的一张表。 整型类型 byte类型 byte类型

    2024年02月01日
    浏览(68)
  • 第02章_变量与运算符拓展练习

    1、辨别标识符 是否符合规则,即编译是否报错? 是否符合规范?即是否够优雅 (1)以下标识符作为类名是否合适 (2)以下标识符作为变量名是否合适 2、数据类型转换简答 3、判断如下代码的运行结果(难) 4、判断如下程序的运行结果 5、判断如下程序的运行结果 6、Java的

    2024年02月02日
    浏览(34)
  • C# 运算符详解:包含算术、赋值、比较、逻辑运算符及 Math 类应用

    运算符用于对变量和值执行操作。在C#中,有多种运算符可用,包括算术运算符、关系运算符、逻辑运算符等。 算术运算符用于执行常见的数学运算: 递增运算符 ++ 用于将变量的值增加 1,而递减运算符 -- 用于将变量的值减少 1: 在实际编码中,请注意避免在表达式中使用

    2024年01月20日
    浏览(53)
  • C# 三目运算符

    C# 三目运算符是一种简单的条件语句,也称为条件运算符。它可以根据一个表达式的结果确定另一个表达式的值。三目运算符使用“?”和“:”来表示。 三目运算符的语法如下: 其中,condition 是一个布尔表达式,expression1 和 expression2 是两个可能返回不同类型值的表达式。

    2024年02月15日
    浏览(44)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包