&& 与(有0为0)
|| 或(有1为1)
! 非
&& 与(有0为0)
|| 或(有1为1)
! 非
逻辑运算符必须是一个布尔值或者布尔表达式
int age = Convert.ToInt32(Console.ReadLine());
bool bl = age >=18 && age <= 60;
Console.WriteLine("是否为青年"+bl);
逻辑运算符
&&(和)称为逻辑运算符。如果两个操作数都非零,则条件为真。 (A&&B)为假。
|| (或)称为逻辑运算符,如果两个操作数中有任意一个非零,则条件为真。 (A||B)为真。
!(非) 称为逻辑运算符,用来逆转操作数逻辑状态,如果条件为真则逻辑非运算符将使其为假。
!(A&&B)为真。
逻辑运算符:
&&同时成立
bool a =(2>1)&&(3>2) true
||满足一条
bool a =(2>1)||2>3) true
!是否不满足
bool a=!(2>1) true
课堂随笔
int a = Convert.ToInt32(Console.ReadLine()), b = Convert.ToInt32(Console.ReadLine()),c = Convert.ToInt32(Console.ReadLine()), d = Convert.ToInt32(Console.ReadLine());
bool e = (a > b) && (d > c);//a=5,b=10,c=8,d=20.需要两侧结果同时满足,所以结果为flase
bool f = (a > b) || (d > c);//a=5,b=10,c=8,d=20.需要任意一侧结果满足,所以结果为ture
bool g = !(a > b);//判断结果是否不成立,结果是不成立的,所以输出结果是true.
//Console.WriteLine("12是否等于13:{0},\n12是否大于13:{1},\n12是否小于13:{2},\n12是否大于等于13:{3},\n12是否小于等于13:{4}",a,b,c,d,e);
Console.WriteLine("当前输入返回值,a={0},b={1},c={2},d={3}\n布尔结果e为{4}\n布尔结果f为{5}\n布尔结果g为{6}",a,b,c,d,e,f,g);
Console.WriteLine("输入一个人的年龄,判断这个人是否是具有劳动力");
int a=Convert.ToInt32(Console.ReadLine());
int b = 18;
int c = 60;
bool d = (a>=18)&&(a<=60) ;
Console.WriteLine(d);
A = true B =false
(A&&B)= FALSE
(A||B)= TRUE
!(A&&B)=TRUE
using System;
namespace _010_逻辑运算符
{
class Program
{
static void Main(string[] args)
{
// &&和 ||或 !非
//bool a = (3 < 4) && (9 < 10);// true && true
bool a = true && true;
bool b = (3 < 2) || (9 < 7);
//bool c = !(4 < 7);
bool c = !true;//取反
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
}
}
}
&& 同时满足
|| 任意一个满足
!不满足则为zhen
逻辑运算符
0.bool函数,用于判断运算结果为Ture 或者 False
1.逻辑运算符一共有三类,逻辑与“&&”;逻辑或“||”;以及逻辑非“!”
2.“&&”逻辑与代表需要同时两个条件时,为Ture,例:(A&&B)即条件A与B都打成时为Ture;
3.“||”逻辑或代表直须满足一个条件,为Ture,例:(A||B)即满足条件A或条件B时,为Ture
4.“!”逻辑非则是逆转操作数的逻辑状态,如果条件为Ture则逻辑非运算符使其为False。例:bool c = !(4 < 7);运算为false,但是逻辑非使其运算结果为Ture
bool a=(3<4)&&(9>6);
bool b=(3<4)||(9>6);
bool c=!(9>6);
bool d=!true;