在真实世界中,大家都很爱比较,我比你有钱,
我长得比你高,我比你帅,
我比你漂亮,C#世界也是非常爱比较的
运算子 | 用途 | 类别 |
---|---|---|
< | 小於 | 二元 |
> | 大於 | 二元 |
<= | 小於等於 | 二元 |
>= | 大於等於 | 二元 |
== | 等於 | 二元 |
!= | 不等於 | 二元 |
注意! 各位千万不要把 " == " 与 " = "搞混了,前者是比较,後者是赋值
逻辑表达式 | 结果 |
---|---|
1 < 2 | 真 |
3 > 2 | 真 |
4 == 5 | 假 |
(6 / 2) != 3 | 假 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//直接印出结果
Console.WriteLine(1 < 2);
Console.WriteLine(3 > 2);
Console.WriteLine(4 == 5);
Console.WriteLine((6 / 2) != 3);
Console.ReadKey();
}
}
}
结果:
True
True
False
False
里头包含了:
&&
且 (and)||
或 (or)!
非 (not)&&
且 (and) :两者条件都要满足P | Q | P and Q |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//宣告x为12
int x = 12;
//x要大於10,因为是偶数所以可以被2整除
Console.WriteLine((x > 10) && (x % 2 == 0));
Console.ReadKey();
}
}
}
结果:
True
||
或 (or):其中之一满足则成立P | Q | P and Q |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//宣告x为12
int x = 12;
//x是正数,但x是三的倍数
Console.Write("x = " + x+" , ");
Console.WriteLine((x < 10) || (x % 3 == 0));
//x是负数,但x不是三的倍数
x = -10;
Console.Write("x = " + x + " , ");
Console.WriteLine((x < 10) || (x % 3 == 0));
//x是正数,也x不是三的倍数
x = 10;
Console.Write("x = " + x + " , ");
Console.WriteLine((x < 10) || (x % 3 == 0));
Console.ReadKey();
}
}
}
结果:
x = 12 , True
x = -10 , True
x = 10 , False
!
非 (not):与事实相反1 != 2
也可以使用 !(1 == 2)
表示bool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CsharpDemo
{
class Program
{
static void Main(string[] args)
{
//宣告一个布林变数
bool isEven;
Console.WriteLine("来输入一个整数吧!");
int x = Convert.ToInt32(Console.ReadLine());
//判断是不是偶数
isEven = (x % 2) == 0;
//列印结果
Console.Write("请问神奇一只鸡," + x + "是偶数吗 ? ");
Console.WriteLine(isEven);
Console.ReadKey();
}
}
}
输入:
10
结果:
来输入一个整数吧!
10
请问神奇一只鸡,10是偶数吗 ? True
输入:
11
结果:
来输入一个整数吧!
1
请问神奇一只鸡,1是偶数吗 ? False
>>: 从 JavaScript 角度学 Python(6) - 变数作用域
结合先前的 产生channel access token 设定heroku 可以开始建立一个服务器接...
动机 决定题目的动机是,从最早开始决定就读资讯相关就想着要在未来踏入电脑游戏产业,而作为许多初学者最...
该文章同步发布於:我的部落格 今天一样继续来介绍实用的 Matcher 和举例~ all matc...
软件架构设计原则 1 - SRP 单一职责原则 在软件设计有个 『 SOLID 原则 』的几个原则,...
tags: ItIron2021 Javascript 作者碎碎念 当时在用这一系列题目跑模拟面试活...