C#表達式和運算符詳細解析
類型轉(zhuǎn)換
Convert.To類型()
1、表達式
將變量和字面值(在使用運算符時,他們都稱作操作數(shù))與運算符組合起來就得到了表達式,它是計算的基本構(gòu)建
簡單的操作包括所有的基本書序操作,如加減乘除;還有專門用于處理布爾值的邏輯運算以及賦值運算。
比如表達式:c=a+b
讓用戶輸入他的語文和數(shù)學(xué)成績,計算他的總成績
using System; namespace 表達式 { class Program { static void Main(string[] args) { int a = 5; int b = 10; int c = a + b; Console.WriteLine(c); Console.WriteLine("請輸入你的語文成績"); double chinese = Convert.ToDouble( Console.ReadLine()); //接收用戶輸入(字符串),并轉(zhuǎn)換成Dobule類型 Console.WriteLine("請輸入你的數(shù)學(xué)成績"); double math = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("您的總成績是{0},平均成績是{1}", chinese+math, (math+chinese)/2); // 字符串連接方式可以用“+” Console.ReadKey(); } } }
1.2 運算符分類
按操作數(shù)的個數(shù)
- 一元運算符:處理一個操作數(shù)(int a=10)
- 二元運算符:處理兩個操作數(shù) a>b
- 三元運算符:處理三個操作數(shù) (?:
按運算類型:
- 數(shù)學(xué)運算符
- 賦值運算符
- 關(guān)系運算符
- 布爾原酸符
- 位運算符 (按為取反)
- 其他運算符(is as)
- var1=var2++ :先用后加,var1等于var2,var2的值加1
- var1=++var2:先加后用(va2=)
2、數(shù)學(xué)運算符
- var2=10
- var1=var2++ 先用后加(var1=var2=10, var2的值加一(var2=10+1))
- var1=++var2 先加后用 (var2加一(var2=10+1),var1等于var2+1)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 數(shù)學(xué)運算符 { class Program { static void Main(string[] args) { int a = 5; int b = 3; int c = a / b; double d = a / b; double e = 5.0; double f = 3.0; double g = e / f; Console.WriteLine("c的值是{0},d的值是{1},g的值是{2}", c, d,g); Console.ReadKey(); } } }
結(jié)果:c的值是1,d的值是1,g的值是1.66666666666667
++/–
3、賦值運算符
4、關(guān)系運算符
5、布爾運算符
注:&&/||與&/|區(qū)別:&&/||可用于對數(shù)值執(zhí)行操作,實際上,他們處理的是在儲存在變量中的一系列位而不是變量的值
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 布爾運算符 { class Program { static void Main(string[] args) { //&與運算符,||或運算符,!取反操作符,^異或運算符 int a = 10; int b = 15; Console.WriteLine((10==a++)||(16==b--)); Console.WriteLine("a的值是{0},b的值是{1}",a,b); //輸出結(jié)果是a的值是11,b的值是15 //或運算符||前一個操作為true時,不在執(zhí)行后一個操作內(nèi)容,所是上面的結(jié)果 Console.ReadKey(); } } }
6、位運算符
位(bit):在計算機中存儲所有數(shù)據(jù)都采用二進制,那么二進制的為,便是我們所有的bit
1Byte=8bit 1字節(jié)=8位
1k=1024Byte
1M=1024k
1G=1024M
1T=1024G
在大俗代碼中都不適用這些運算符,但應(yīng)知道這樣的運算符存在。它們主要用于高度優(yōu)化的代碼,在這些代碼中,使用其他數(shù)學(xué)操作的開銷太高。因此它們通常用于驅(qū)動程序或系統(tǒng)代碼
~ 按位取反 &與運算 |或運算 ^異或 <<左移 >>右移
6.1 &按位與運算
6.2 或|按位運算
6.3 異或^按位運算符
6.4按位取反~按位預(yù)算符
6.5 左移<<運算符
var3=var1<<var2 var1向左移動var2位,將所得的值付給var3
6.6右移
var3=var1>>var2 var1向右移動var2位,將所得的值付給var3
左移一位,相當(dāng)于乘以2;
右移一位,當(dāng)當(dāng)與除以2;然后去除非整數(shù)部分
7、其他運算符
7.1 字符連接運算符+
將兩個字符串連接在一起組成一個新的字符串
備注:C#中“+”共三種用法
- 數(shù)學(xué)算數(shù)運算符 var1=var2+var3
- 乘以正1 var1 =+var2 (var1=var2*(+1))
- 字符串連接 str1=“my name is” str2=“darly” str3=str1+" "+str2,則str3=“my name is darly” 7.2 is運算符
用于動態(tài)檢查對象的運行時類型是否與給定類型兼容
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 其他運算符 { class Program { static void Main(string[] args) { int a = 10; double b = 3.14; string c = "it is a string"; bool mybool = false; mybool = a is int; Console.WriteLine("a is int\t"+mybool); // \t是跳轉(zhuǎn)到下一個制表符 mybool = b is double; Console.WriteLine("b is doubule\t" + mybool); mybool = c is string; Console.WriteLine("c is string\t" + mybool); Console.ReadKey(); } } }
7.3 三元運算符
表達式1?表達式2:表達式3 表達式1為true則結(jié)果為表達式2,否則結(jié)果為表達式3
輸出語句“I have 數(shù)量 pen” 當(dāng)數(shù)量為1時為pen,數(shù)量大于1時為pens
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 其他運算符 { class Program { static void Main(string[] args) { Console.WriteLine("請輸入您擁有的鋼筆數(shù)量"); int qty = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("I have {0} pen{1}", qty,qty>1?"s":""); //Console.ReadKey(); // 比較用戶輸入數(shù)據(jù)與5的關(guān)系 Console.WriteLine("請您輸入要比較的數(shù)字"); int number1 = Convert.ToInt32(Console.ReadLine()); string result = number1 > 5 ? "大于等于" : "小于"; Console.WriteLine("您輸入的數(shù)字{0}5",result); Console.ReadKey(); } } }
8、運算優(yōu)先級
總結(jié)練習(xí)
編程實現(xiàn)1532855秒是幾天幾小時幾分鐘幾秒
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 總結(jié)練習(xí) { class Program { static void Main(string[] args) { // 方法1,利用取整再取余的方式逐級獲取響應(yīng)的單位值 Console.WriteLine("請輸入你要計算的秒數(shù)"); int day = 24 * 60 * 60; int hour = 60 * 60; //int total = 1232855; int total = Convert.ToInt32(Console.ReadLine()); int days = total / day; //計算天數(shù) 利用總秒數(shù)對一天一共多少秒取整,得到天數(shù) int hours = (total % day) / hour; // 利用總秒數(shù)對一天一共多少秒取余數(shù),得到不夠一天的秒數(shù),然后對一小時包含秒數(shù)整,得到小時數(shù); int minutes = (total % day % hour)/60; //同樣思路,利用兩次取余得到不足一小時的數(shù)量,再對60秒取整得到分鐘數(shù) int minutes2 = total % 3600 / 60; //計算分鐘的方式2,總秒數(shù)對一小時取余得到不足一小時的秒數(shù)然后取整60秒得到分鐘數(shù) int second = total % 60; //用總數(shù)對60秒取余得到不足一分鐘的秒數(shù),就是秒 Console.WriteLine("{0}天{1}小時{2}分鐘{3}秒",days,hours,minutes,second); Console.WriteLine(minutes2+"分鐘"); Console.ReadLine(); } } }
到此這篇關(guān)于C#表達式和運算符詳細解析的文章就介紹到這了,更多相關(guān)C#表達式和運算符內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用CryptoStream類加密和解密字符串的實現(xiàn)
CryptoStream設(shè)計用于在內(nèi)容以流的形式輸出到文件時加密和解密內(nèi)容,本文主要介紹了C#使用CryptoStream類加密和解密字符串的實現(xiàn),具有一定的參考價值,感興趣的可以了解一下2024-01-01c# winform取消右上角關(guān)閉按鈕的實現(xiàn)方法
本文是對c#中winform取消右上角關(guān)閉按鈕的實現(xiàn)方法進行了詳細的介紹,需要的朋友可以過來參考下。希望對大家有所幫助2013-10-10c#中l(wèi)ist.FindAll與for循環(huán)的性能對比總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于c#中l(wèi)ist.FindAll與for循環(huán)的性能,文中通過詳細的示例代碼給大家介紹了這兩者之間的性能,對大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案
在WPF中,默認情況下,DataContext是通過可視化樹來傳遞的,父元素的DataContext會自動傳遞給其子元素,以便子元素可以訪問父元素的數(shù)據(jù)對象,這篇文章主要介紹了WPF中的ValidationRule實現(xiàn)參數(shù)綁定解決方案,需要的朋友可以參考下2023-08-08