算数运算符
运算符
+
-
*
/
%
练习
int csharp = 90;
int math = 85;
int chinese = 100;
int sum = csharp + math + chinese;
int average = sum / 3;
Console.WriteLine("成绩综合为{0},成绩平均值为{1}", sum, average);
Console.ReadLine();
自增自减运算符
运算符
n++
++n
n--
--n
练习
/*int n = 10;
int result = 10 + n++;
Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=11;result=20
Console.ReadLine();*/
//int n = 10;
//int result = 10 + ++n;
//Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=11;result=21;
//Console.ReadLine();
//int n = 10;
//int result = 10 + n--;
//Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=9;result=20;
//Console.ReadLine();
int n = 10;
int result = 10 + --n;
Console.WriteLine("n的值为{0},result的值为{1}", n, result);
//n=9;result=19;
Console.ReadLine();
赋值运算符
运算符
=
+=
等
练习
int a = 10;
int b=20;
b += a;//b=20+10
a = b;//a=b=30
Console.WriteLine("a的值为{0},b的值为{1}", a, b);
Console.ReadLine();
关系运算符
运算符
>
<
==
>=
<=
!=
逻辑运算符
运算符
&&:同真为真
||:有真为真
!
练习
Console.WriteLine("打出年份");
int year=Convert.ToInt32(Console.ReadLine());
bool flags=(year%400==0||year%4==0||year%100!=0);
Console.WriteLine(flags);
Console.ReadLine();
位运算符
运算符
&:位与:a和b对应为同时位1,结果为才为1,否则为0;
|:位或:a和b对应为都是0,结果位才为0,否则为1
^:位异或:同时为0或1时,结果为0,否则为1;
~:取反
移位运算符
运算符
<<:左移位运算符
>>:右移位运算符
条件运算符
表达式
a>b?a:b
练习
Console.WriteLine("打出年龄");
int year=Convert.ToInt32(Console.ReadLine());
string number = year >= 18 ? "你成年了" : "青少年";
Console.WriteLine(number);
Console.ReadLine();
关键
运算符是有优先级的