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