程序的世界中,变数是无时无刻一直在变化的,
变数的变化也成为程序的精随所在,
但为甚麽变数会一直变化呢?
原来一切都是运算子惹的祸。
运算子 | 用途 | 范例 | 类别 |
---|---|---|---|
+ | 加 | a + b | 二元 |
- | 减 | a - b | 二元 |
* | 乘 | a * b | 二元 |
/ | 除 | a / b | 二元 |
% | 取余数 | a % b | 二元 |
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)
{
//宣告今年
int y = 2021;
//宣告今月
int m = 9;
//宣告今日
int d = 6;
//套用基姆拉尔森计算公式
int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400 + 1) % 7;
//印出日期与星期
Console.WriteLine("2021/9/6 星期" + week);
Console.ReadKey();
}
}
}
结果:
2021/9/6 星期1
在这公式中我们一口气用到了五个运算子,也体会到C#让人十分惊艳的运算
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)
{
//做三除以二的运算
int ans = 3 / 2;
//印出解答
Console.WriteLine("Ans : " + ans);
Console.ReadKey();
}
}
}
结果:
Ans : 1
向0无条件舍去
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)
{
//做整数除法,并赋值给浮点数变数
double x = 9 / 2;
//直接做浮点数除法
double y = 9.0 / 2;
//印出答案
Console.WriteLine("x = " + x + " ,y = " + y);
Console.ReadKey();
}
}
}
结果
x = 4 ,y = 4.5
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)
{
//分别将20~25与4做取余运算
int no20 = 20 % 4;
int no21 = 21 % 4;
int no22 = 22 % 4;
int no23 = 23 % 4;
int no24 = 24 % 4;
int no25 = 25 % 4;
//印结果
Console.WriteLine("20 % 4 =" + no20);
Console.WriteLine("21 % 4 =" + no21);
Console.WriteLine("22 % 4 =" + no22);
Console.WriteLine("23 % 4 =" + no23);
Console.WriteLine("24 % 4 =" + no24);
Console.WriteLine("25 % 4 =" + no25);
Console.ReadKey();
}
}
}
结果:
20 % 4 =0
21 % 4 =1
22 % 4 =2
23 % 4 =3
24 % 4 =0
25 % 4 =1
功能 | 方法 | 范例 | 数学表示 |
---|---|---|---|
乘幂 | Math.Pow() | Math.Pow(2,3) | 2^3 |
平方根 | Math.Sqrt() | Math.Sqrt(4) | √4 |
绝对值 | Math.Abs() | Math.Abs(-4) | |-4| |
数学涵义 | 程序表示 | 近似值 | 说明 |
---|---|---|---|
τ |
Math.Tau | 6.2831853071795862 | 弧度 |
π |
Math.PI | 3.1415926535897931 | 圆周率 |
e |
Math.E | 2.7182818284590451 | 自然对数底数 |
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)
{
//宣告上底
int x = 30;
//宣告下底
int y = 52;
//宣告高
int z = 46;
//进行计算
int ans = (x + y) * z / 2;
//印出答案
Console.WriteLine("Ans : " + ans);
Console.ReadKey();
}
}
}
结果:
Ans : 1886
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)
{
//宣告我今年48岁
int age = 48;
Console.WriteLine("我今年" + age + "岁");
//使用後置递增运算子,计算明年年纪
age++;
Console.WriteLine("我明年" + age + "岁");
Console.ReadKey();
}
}
}
结果:
我今年48岁
我明年49岁
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("--後置运算子--");
int x = 1;
Console.WriteLine(x);
Console.WriteLine(x++);
Console.WriteLine(x);
//前置递增运算子,印出它的变化过程
Console.WriteLine("--前置运算子--");
int y = 1;
Console.WriteLine(y);
Console.WriteLine(++y);
Console.WriteLine(y);
Console.ReadKey();
}
}
}
结果:
--後置运算子--
1
1
2
--前置运算子--
1
2
2
由此我们可以看到前置跟後置的差异会是,在运算「之後」或「之前」某数的值
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)
{
//递减运算子
int x = 5;
//後置
Console.WriteLine(x--);
//前置
Console.WriteLine(--x);
Console.ReadKey();
}
}
}
结果:
5
3
大家可能会觉得,这结果也太奇怪了吧?!
为什麽不是5,4呢?
原因很简单,因为我们先用後置递减运算子,所以他是在输出5之後,x变成4了,
接下来使用前置递减运算子,所以4先被减成3,然後才输出
功能 | 范例 | 等值写法 | 类别 |
---|---|---|---|
+= |
x += 100 | x = x + 100 | 二元 |
-= |
x -= 100 | x = x - 100 | 二元 |
*= |
x *= 100 | x = x * 100 | 二元 |
/= |
x /= 100 | x = x / 100 | 二元 |
%= |
x %= 100 | x = x % 100 | 二元 |
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("--整数版--");
int x = 10;
x += 100;
Console.WriteLine(x);
//加法指派运算子 += (字串版)
Console.WriteLine("--字串版--");
string name = "孤独一只鸡";
name += " 史上最帅!";
Console.WriteLine(name);
Console.ReadKey();
}
}
}
结果:
--整数版--
110
--字串版--
孤独一只鸡 史上最帅!
()
做使用,这样能增加程序的可读性
<<: #6 CSS Table x Stock Price
阿嬷都看得懂的前端与後端怎麽分 首先,准备一个阿嬷-民明书房《阿嬷的古早味卤肉饭怎麽煮》 阿嬷的乖孙...
前情提要 艾草:「不知不觉也累积了不少魔力总量了,我们今天透过魔法阵列来找出你适合的属性值吧!」 「...
Flutter架构 (一)Framework:由纯Dart语言实现的SDK 1.底下两层:底层UI函...
本篇开始终於要进入解决需求的前置作业了!首先需要让网站有个基本的置顶导览列,让我们有请 Bootst...
今天来讲讲 Ansible。记得我在第二天的时候曾经讲过,自动化在 SRE 里面是很重要的一环。让机...