C#学习笔记,2021/12/2
关系运算符
> < >= <= == !=
概念:关系运算符是用来描述两个事物之间的关系。
关系运算符连接的表达式称之为“关系表达式”。
关系表达式运算结果只有对错,所以关系表达式是bool类型的。
bool b = 150 > 100;
Console.WriteLine(b);
Console.ReadKey();//输出结果为Ture
Console.WriteLine(b);
Console.ReadKey();//输出结果为Ture
逻辑运算符
&&逻辑与
&&两边的表达式结果都为ture的时候,这个罗技与表达式的结果就为Ture;
两边的表达式结果只要有一个是false,那么整个逻辑表达式的结果就是false。
bool b = 140 < 200 && 2>1;
Console.WriteLine(b);
Console.ReadKey();//结果为Ture。
Console.WriteLine(b);
Console.ReadKey();//结果为Ture。
||逻辑或
||两边的表达式结果只要有一遍为Ture,整个表达式的结果就为Ture。
两边的表达式只有都为False时,整个表达式的结果才为False。
bool b = 140 < 200 ||2 < 1;
Console.WriteLine(b);
Console.ReadKey();//输出结果为Ture
Console.WriteLine(b);
Console.ReadKey();//输出结果为Ture
!逻辑非
!真的变假,假的变真。(后边跟一个表达式并且带括号)
bool b = !(5 > 6);
Console.WriteLine(b);
Console.ReadKey();//输出为Ture
Console.WriteLine(b);
Console.ReadKey();//输出为Ture
小测试

Console.WriteLine("请输入您的年龄:");
string age = Console.ReadLine();
int strage = Convert.ToInt32(age);
Console.WriteLine("请输入您的长度:");
string lon = Console.ReadLine();
int strlon = Convert.ToInt32(lon);
bool min = strage > 18 && strlon > 18;
Console.WriteLine(min);
Console.ReadKey();
string age = Console.ReadLine();
int strage = Convert.ToInt32(age);
Console.WriteLine("请输入您的长度:");
string lon = Console.ReadLine();
int strlon = Convert.ToInt32(lon);
bool min = strage > 18 && strlon > 18;
Console.WriteLine(min);
Console.ReadKey();

Console.WriteLine ("请输入你的语文成绩:");
string chinese = Console.ReadLine();
int strchinese = Convert.ToInt32(chinese);
Console.WriteLine("请输入你的英语成绩:");
string english = Console.ReadLine();
int strenglish = Convert.ToInt32(english);
bool mix = strchinese > 90 && strenglish > 90;
Console.WriteLine(mix);
Console.ReadKey();
string chinese = Console.ReadLine();
int strchinese = Convert.ToInt32(chinese);
Console.WriteLine("请输入你的英语成绩:");
string english = Console.ReadLine();
int strenglish = Convert.ToInt32(english);
bool mix = strchinese > 90 && strenglish > 90;
Console.WriteLine(mix);
Console.ReadKey();

Console.WriteLine ("请输入你的语文成绩:");
string chinese = Console.ReadLine();
int strchinese = Convert.ToInt32(chinese);
Console.WriteLine("请输入你的英语成绩:");
string english = Console.ReadLine();
int strenglish = Convert.ToInt32(english);
bool mix = strchinese > 90 || strenglish > 90;
Console.WriteLine(mix);
Console.ReadKey();
string chinese = Console.ReadLine();
int strchinese = Convert.ToInt32(chinese);
Console.WriteLine("请输入你的英语成绩:");
string english = Console.ReadLine();
int strenglish = Convert.ToInt32(english);
bool mix = strchinese > 90 || strenglish > 90;
Console.WriteLine(mix);
Console.ReadKey();
可以简化为
Console.WriteLine("请输入你的语文成绩:");
int chinese = Convert.ToInt32(Console.ReadLine());//此处将赋值和转换过程一体化了
Console.WriteLine("请输入你的英语成绩:");
int english = Convert.ToInt32(Console.ReadLine());//此处将赋值和转换过程一体化了
bool mix = chinese > 90 || english > 90;
Console.WriteLine(mix);
Console.ReadKey();
int chinese = Convert.ToInt32(Console.ReadLine());//此处将赋值和转换过程一体化了
Console.WriteLine("请输入你的英语成绩:");
int english = Convert.ToInt32(Console.ReadLine());//此处将赋值和转换过程一体化了
bool mix = chinese > 90 || english > 90;
Console.WriteLine(mix);
Console.ReadKey();

Console.WriteLine("判断闰年程序,请输入你想要判断的年份,是则为Ture,不是则为False");//1闰年能被4整除但是不能被100整除,2能被400整除的。
int year = Convert.ToInt32(Console.ReadLine());
bool b = (year % 4 == 0 && year % 100 != 0) || (year%400 == 0);
Console.WriteLine(b);
Console.ReadKey();
int year = Convert.ToInt32(Console.ReadLine());
bool b = (year % 4 == 0 && year % 100 != 0) || (year%400 == 0);
Console.WriteLine(b);
Console.ReadKey();

浙公网安备 33010602011771号