1 关系运算符
< >= <= == !=
关系表达式的运算结果只有对错,所以关系表达式就是bool类型
2 逻辑运算符
-
&&与
&&两边表达式结果都为true,这个逻辑与表达式结果为true;
&&两边表达式结果只要有一个为false,这个逻辑与表达式结果为false; -
|| 或者
|| 两个表达式只要有一个成立,整体表达式都是true;
|| 只有两个表达式都为false,整体表达式才是false; -
!非
真的变假的,假的变真的
static void Main(string[] args)
{
bool b = !(5 > 3);
Console.WriteLine(b);
Console.ReadKey();------false
}
逻辑运算符两边放的一般都是关系表达式或者bool类型的值;
逻辑表达式的结果同样也是bool类型;
3 练习
Console .WriteLine("请输入你的语文成绩");
string strchinese = Console.ReadLine();//申明变量去接收,数据存储在strchinese中;
int chinese = Convert.ToInt32(strchinese);//转换它为int;
简化法:
static void Main(string[] args)
{
Console .WriteLine("请输入你的语文成绩");
int chinese = Convert.ToInt32(Console.ReadLine());
}
&& 与----两边都必须成立表达式结果为true,否则一边成立一边不成立为false
static void Main(string[] args)
{
Console .WriteLine("请输入你的语文成绩");
int chinese = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你的数学成绩");
int math = Convert.ToInt32(Console.ReadLine());
//如果语文与数学成绩都大于90
bool b = chinese > 90 && math > 90;
Console.WriteLine(b);
Console.ReadKey();
}
|| 或者,一边成立就为true;两边都不成立为false
static void Main(string[] args)
{
Console .WriteLine("请输入你的语文成绩");
int chinese = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入你的数学成绩");
int math = Convert.ToInt32(Console.ReadLine());
//如果语文与数学成绩都大于90
bool b = chinese > 90 || math > 90;
Console.WriteLine(b);
Console.ReadKey();
}