Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using System;
|
||||
namespace Lesson12_逻辑运算符
|
||||
{
|
||||
class program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识点一 逻辑与
|
||||
//符号&&
|
||||
//规则:对两个bool值进行逻辑与运算,只有当两个值都为true时,结果才为true,否则为false。
|
||||
bool result = true && false;
|
||||
Console.WriteLine(result); // 输出: False
|
||||
result = true && true;
|
||||
Console.WriteLine(result); // 输出: True
|
||||
|
||||
//bool相关类型 bool变量 条件运算符
|
||||
//逻辑运算符优先级 低于 条件运算符 和 关系运算符
|
||||
result = 3 > 1 && 1 < 2;// 先计算关系运算符,再计算逻辑运算符
|
||||
Console.WriteLine(result);// 输出: True
|
||||
|
||||
int i = 3;
|
||||
result = i > 1 && i < 5; // 先计算关系运算符,再计算逻辑运算符
|
||||
Console.WriteLine(result); // 输出: True
|
||||
#endregion
|
||||
|
||||
#region 知识点二 逻辑或
|
||||
//符号||
|
||||
//规则:对两个bool值进行逻辑或运算,只要有一个值为true,结果就为true,否则为false。
|
||||
result = true || false;
|
||||
Console.WriteLine(result);// 输出: True
|
||||
result = false || true;
|
||||
Console.WriteLine(result);// 输出: True
|
||||
result = true || true;
|
||||
Console.WriteLine(result);// 输出: True
|
||||
result = false || false;
|
||||
Console.WriteLine(result);// 输出: False
|
||||
#endregion
|
||||
|
||||
#region 知识点三 逻辑非
|
||||
//符号!
|
||||
//规则:对一个bool值进行取反,真变假,假变真
|
||||
result = !true;
|
||||
Console.WriteLine(result);// 输出:false
|
||||
result = !false;
|
||||
Console.WriteLine(result);// 输出:true
|
||||
result = !!false;
|
||||
Console.WriteLine(result);// 输出:false
|
||||
|
||||
//result = !3 > 2;
|
||||
//逻辑非的优先级较高会直接报错必须要套括号
|
||||
result = !(3 > 2);
|
||||
Console.WriteLine(result);// 输出:false
|
||||
#endregion
|
||||
|
||||
#region 知识点四 混合使用优先级
|
||||
//规则 !(逻辑非)的优先级最高,&&(逻辑与)的优先级高于||(逻辑或)
|
||||
//逻辑运算符优先级 低于 算术运算符 条件运算符
|
||||
|
||||
bool gameOver = false;
|
||||
int hp = 100;
|
||||
bool isDead = false;
|
||||
bool isMustOver = true;
|
||||
|
||||
result = gameOver || hp < 0 && !isDead || isMustOver;
|
||||
Console.WriteLine(result);//输出 True
|
||||
#endregion
|
||||
|
||||
#region 知识点五 逻辑运算符短路规则
|
||||
int i3 = 1;
|
||||
//短路
|
||||
result = i3 > 0 || ++i3 >= 1;//i3此时不会+1为2
|
||||
Console.WriteLine(i3);
|
||||
Console.WriteLine(result);//输出true
|
||||
//在左边i3 > 0运算后,已经出现了boolean值,后面的表达式不会计算了
|
||||
//概念:只要逻辑与 或者 逻辑或 左边已经满足了条件,后面的结果不会继续运算了
|
||||
result = ++i3 >= 1 && i3 < 0;//i3此时会先进行运算
|
||||
Console.WriteLine(i3);
|
||||
Console.WriteLine(result);//输出false
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user