C# 中的数字

2023年 9月 16日 50.3k 0

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

登录后复制

以上就是C# 中的数字的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论