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();
}
}
}