首頁  >  文章  >  後端開發  >  C# 中的數字

C# 中的數字

WBOY
WBOY轉載
2023-09-15 23:13:021327瀏覽

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# 中的數字的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除