C#學習筆記---異常捕獲和變數運算子

暢知發表於2023-10-09

異常捕獲

使用異常捕獲可以捕獲出現異常的程式碼塊,防止因為異常丟擲造成的程式卡死的情況發生。

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#程式碼不迷路!

相關文章