日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#算数运算符、关系运算符、逻辑运算符、语句

發布時間:2024/8/24 C# 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#算数运算符、关系运算符、逻辑运算符、语句 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C#算數運算符、關系運算符、邏輯運算符、語句

一、運算符:

(一)、算術運算符:+?-?*?/%

%?——取余運算取余運算的應用場景:

1.奇偶數的區分。

2.把數變化到某個范圍之內。——彩票生成。

3.判斷能否整除。——閏年、平年。

int a = 10, b = 3; Console.WriteLine("10/3=" + (a / b)); Console.WriteLine("10%3=" + (a % b));

++(自增運算)?--(自減運算)——它只能對變量進行運算。

int a = 5; a++; Console.WriteLine(a);//a = 6;

1.前自增/前自減
先進行自增/自減運算,然后再進行其它運算。可以簡單認為前自增/前自減的優先級是最高。

int a = 5,b; b = ++a; Console.WriteLine("a=" + a + ";b=" + b); //結果:a=6,b=6

2.后自增/后自減
先進行其它運算,當其它運算都完成后,再進行自增/自減運算。可以簡單認為是后自增/后自減優先級是最低的。

int a = 5,b; b = a++; Console.WriteLine("a=" + a + ";b=" + b);//結果:a=6,b=5

?

(二)、關系運算符:——用來判斷式子成立與否

==!=>>=<<=

?


注意:
雙等號不要寫成單等號

?

(三)、邏輯運算符:

&&?與,||或,!非

&&和||是雙操作數,!是單操作數

int a = 5,b=6; Console.WriteLine(a > b && a > 0); //false; Console.WriteLine(a <b && a > 0);//true

?

int a = 5,b=6; Console.WriteLine((a > b) || (a > 0)); //true Console.WriteLine((a > b) || (a < 0));//false

?

!?非?——取反

優先級:
一般來說:
1.算術運算術的優先級要高關系運算符;關系運算符的優先級要高于邏輯運算符
2.邏輯非優先級最高。邏輯與要高于邏輯或。
3.如果不確定,就加小括號。

(四)、其它運算符:

1.賦值運算符=。把右邊的結果送到左邊去。左邊只能是變量。

2.復合運算符+=?-=?*=?/=?%=?知道就行。a+=5;?<==>?a?=?a?+?5

3.條件運算符三目運算符?:

int?a=5,b=6,c;

c?=?a?>?b???a?:?b;

Console.WriteLine(?c?)//6

作業:

1.游泳?一游泳場地,呈圓形,泳池半徑a,廣場半徑ba<b),

求泳池護欄長度求廣場磚面積護欄造價25/米,廣場磚85/m^2,求護欄、廣場磚共花費多?少?

Console.Write("請輸入泳池半徑a:"); string a = Console.ReadLine(); Console.Write("請輸入廣場半徑b:"); string b = Console.ReadLine(); const double PI = 3.1415926; double L = 2 * PI * Convert.ToDouble(a); double S1 = PI * Convert.ToDouble(b)* Convert.ToDouble(b)- PI * Convert.ToDouble(a) * Convert.ToDouble(a); double RMB = 25 * L + 85 * S1; Console.WriteLine("泳池護欄長度L:"+L+"m"); Console.WriteLine("廣場磚面積S1:" + S1 + "m^2"); Console.WriteLine("護欄、廣場磚共花費YMB:"+RMB+"");

?

?


2.老狼老狼幾點了

Console.Write("老狼老狼幾點了?"); string hour = Console.ReadLine(); int h, h1; string ap; h = Convert.ToInt32(hour); h1 = h > 12 ? h - 12 : h; ap = h > 12 ? "下午" : "上午"; Console.WriteLine("現在是" + ap + h1 + "點!");

?

?


3.輸入三個數a,b,c。輸出最大的。

int a, b, c, d, max; Console.WriteLine("請輸入三個數"); string a1 = Console.ReadLine(); string b1 = Console.ReadLine(); string c1 = Console.ReadLine(); a = Convert.ToInt32(a1); b = Convert.ToInt32(b1); c = Convert.ToInt32(c1); d = a > b ? a : b; max = d > c ? d : c; Console.WriteLine("最大值是" + max);

?

?

二、語句:順序,分支,循環,跳轉,異常。

(一)順序語句:略
(二)分支語句:

判斷--表達式。if(){}

四大類:
1.if
if?(age?>?18)?
{
Console.WriteLine("可以去當兵!");
}

注意:if表達式后面只管一句話,可以省略掉{};如果if表達式后面需要管多句話,則必須加{}

?

2.if...else...

if (age > 18) { Console.WriteLine("成年了!"); Console.WriteLine("可以去當兵!"); } else { Console.WriteLine("還沒長大!"); Console.WriteLine("回家上學去!"); }

?

注意:
1.else后面不要加分號。
2.else后面不要加小括號。


3.if...else?if...else?if...else?多分支。

//輸入 Console.Write("老狼老狼幾點了?"); string s = Console.ReadLine(); int hour = Convert.ToInt32(s);if (hour >= 0 && hour < 6) // 0<hour<6:錯誤 { Console.WriteLine("凌晨" + hour + "點了"); } else if (hour >= 6 && hour <= 12) { Console.WriteLine("上午" + hour + "點了"); } else if (hour > 12 && hour < 18) { hour -= 12; Console.WriteLine("下午" + hour + "點了"); } else if (hour >= 18 && hour < 24) { hour -= 12; Console.WriteLine("晚上" + hour + "點了"); } else { Console.WriteLine("不可識別的時間!"); }

?


4.if嵌套。

if(...) {if(...){}else{} } else {if(...){} else{} }

?

?

分層、分類來解決問題的思路。


作業:
1.老狼幾點了。凌晨,上午,下午,晚上。

Console.Write("請輸入時間:"); string s = Console.ReadLine(); int h = Convert.ToInt32(s); if (h >= 0 && h < 6) { Console.WriteLine("時間是凌晨" + h + ""); } else if (h >= 6 && h <= 12) { Console.WriteLine("時間是上午" + h + ""); } else if (h > 12 && h <= 18) { h -= 12; Console.WriteLine("時間是下午" + h + ""); } else if (h > 18 && h <= 24) { h -= 12; Console.WriteLine("時間是晚上" + h + ""); } else { Console.WriteLine("輸入時間錯誤"); }

?


2.判斷一元二次方向根的情況。

Console.WriteLine("請輸入方程系數a、b、c"); string s = Console.ReadLine(); string s1 = Console.ReadLine(); string s2 = Console.ReadLine(); int a, b, c; double d; a = Convert.ToInt32(s); b = Convert.ToInt32(s1); c = Convert.ToInt32(s2); d = b * b - 4 * a * c; if (a == 0) { Console.WriteLine("不是一元二次方程"); } else if (d > 0) { Console.WriteLine("有兩個實根"); } else if (d < 0) { Console.WriteLine("沒有實根"); } else { Console.WriteLine("有一個實根"); }

?


3.輸入一個年份,判斷是閏年還是平年。

Console.Write("請輸入年份:"); string s = Console.ReadLine(); int year = Convert.ToInt32(s); if (year > 0 && year < 9999) { if (year % 400 == 0) { Console.WriteLine(year + "年是閏年"); } else if (year % 4 == 00 && year % 100 != 0) { Console.WriteLine(year + "年是閏年"); } else { Console.WriteLine(year + "年是平年"); } } else { Console.WriteLine("輸入年份有問題,請核對"); }

?

?


4.稱體重。
男人的標準體重是:體重(kg)=身高(cm)-100
女人的標準體重是:體重(kg)=身高(cm)-110
上下浮動3公斤屬正常
要求輸入性別、身高和體重,輸出正常,偏胖,偏瘦

Console.Write("請輸入性別:"); string sex = Console.ReadLine(); Console.Write("請輸入身高:"); string h = Console.ReadLine(); Console.Write("請輸入體重"); string w = Console.ReadLine(); int h1, w1, b, n; h1 = Convert.ToInt32(h); w1 = Convert.ToInt32(w); if (sex == "") { n = h1 - 100; b = w1 - n; if (b >= -3 && b <= 3) { Console.WriteLine("體重正常"); } else if (b < -3) { Console.WriteLine("略瘦"); } else { Console.WriteLine("略胖"); }} else if (sex == "") { n = h1 - 110; b = w1 - n; if (b >= -3 && b <= 3) { Console.WriteLine("體重正常"); } else if (b < -3) { Console.WriteLine("體重偏瘦"); } else { Console.WriteLine("偏胖"); } } else { Console.WriteLine("性別輸入錯誤"); }

?

?

//稱體重(方法2)

Console.Write("請輸入性別:"); string sex = Console.ReadLine(); Console.Write("請輸入身高:"); string h = Console.ReadLine(); Console.Write("請輸入體重"); string w = Console.ReadLine(); int h1, w1, b, n=0; h1 = Convert.ToInt32(h); w1 = Convert.ToInt32(w); if (sex == "" || sex == "") { if (sex == "") { n = h1 - 100; } else if (sex == "") { n = h1 - 110; }b = w1 - n; if (b >= -3 && b <= 3) { Console.WriteLine("體重正常"); } else if (b < -3) { Console.WriteLine("略瘦"); } else { Console.WriteLine("略胖"); } } else { Console.WriteLine("性別輸入錯誤"); }

?

?

5.輸入年、月、日,判斷是否是個正確的日期。

Console.Write("請分別輸入年月日"); string year1 = Console.ReadLine(); string month1 = Console.ReadLine(); string day1 = Console.ReadLine();int year = Convert.ToInt32(year1); int month = Convert.ToInt32(month1); int day = Convert.ToInt32(day1); if (year > 0 && year < 9999) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if (day > 0 && day <= 31) { Console.WriteLine("日期正確"); } else { Console.WriteLine("日期錯誤"); } } else if (month == 4 || month == 6 || month == 11 || month == 9) { if (day > 0 && day <= 30) { Console.WriteLine("日期正確"); } else { Console.WriteLine("日期錯誤"); } } else if (month == 2) { if (year % 400 == 0 || year % 4 == 00 && year % 100 != 0) { if (day > 0 && day <= 29) { Console.WriteLine("日期正確"); } else { Console.WriteLine("日期錯誤"); } } else { if (day > 0 && day <= 28) { Console.WriteLine("日期正確"); } else { Console.WriteLine("日期錯誤"); } }} else { Console.WriteLine("日期錯誤"); } } else { Console.WriteLine("日期錯誤"); }

?

?

?

轉載于:https://www.cnblogs.com/wllhq/p/4179095.html

總結

以上是生活随笔為你收集整理的C#算数运算符、关系运算符、逻辑运算符、语句的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。