C# 中的数字
对于 C# 中的数字,使用类型 int。它表示一个整数,即正或负整数。
让我们看看如何使用数学运算符 + − 在 C# 中添加两个整数。
using System;
using System.Linq;
class Program {
static void Main() {
int x = 20;
int y = 30;
int sum = 0;
sum = x + y;
Console.WriteLine(sum);
}
}现在让我们了解这些数学运算符的顺序,即运算符优先级。
运算符优先级确定表达式中各项的分组。这会影响表达式的求值。某些运算符的优先级高于其他运算符;例如,乘法运算符的优先级高于加法运算符。
例如 x = 9 + 2 * 5;此处,向 x 分配 19,而不是 55,因为运算符 * 的优先级高于 +,因此第一个求值为 2*5,然后向其添加 9。
以下是显示运算符顺序的一个示例 −
示例
using System;
namespace Demo {
class Program {
static void Main(string[] args) {
int a = 200;
int b = 100;
int c = 150;
int d = 50;
int res;
res = (a + b) * c / d;
Console.WriteLine("Value of (a + b) * c / d is : {0}", res);
res = ((a + b) * c) / d;
Console.WriteLine("Value of ((a + b) * c) / d is : {0}", res);
res = (a + b) * (c / d);
Console.WriteLine("Value of (a + b) * (c / d) : {0}",res);
res = a + (b * c) / d;
Console.WriteLine("Value of a + (b * c) / d : {0}",res);
Console.ReadLine();
}
}
}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP