C# 《四》表達式和運算符(1)
阿新 • • 發佈:2017-08-20
center key 操作 mes string src ring mage ons
1、表達式
1、將變量和字面值(在使用運算符時,它們都稱作操作數)與運算符組合起來就得到了表達式,它是計算的基本構件。
2、簡單的操作包括所有的基本數學操作,如加減乘除;還有專門用於處理布爾值的邏輯運算以及賦值運算。
3、比如表達式:c = a + b ;
4、問題:讓用戶輸入他的語文和數學成績,計算他的總成績並顯示出來?
Convert.
解:
程序如下
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;namespace _4._1表達式 { class Program { static void Main(string[] args) { //int a = 10; //int b = 6; //int c = a + b; //Console.WriteLine(c); Console.WriteLine("請輸入你的語文成績:"); double Chinese = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("請輸入你的數學成績:"); double Math = Convert.ToDouble(Console.ReadLine()); Console.WriteLine("你的總分是{0}分,你的平均分是{1}分",Math + Chinese,(Math + Chinese)/2); Console.ReadKey(); } } }
運算結果:
運算符分類
1、按操作數的個數:
一元運算符:處理一個操作數;
int a = 10;
二元運算符:處理兩個操作數;
int c =a + b;
三元運算符:處理三個操作數;
int e = a + b + c;
2、按運算類型:
數學運算符、賦值運算符、關系運算符、布爾運算符、位運算符、其他運算符。
C# 《四》表達式和運算符(1)