Arduino 中的取模 / 餘數


Arduino 中的取模運算子與 C 語言完全相同,或者對大多數其他語言來說也是如此。該運算子是 %。語法為:a % b,它返回 a 除以 b 後的餘數。

示例

以下示例說明了該運算子的使用 −

void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   Serial.println();

   Serial.println(10%3);
   Serial.println(4%2);
   Serial.println(50%9);
}
void loop() {
   // put your main code here, to run repeatedly:
}

輸出

序列埠監視器輸出如下所示。你可以自己計算餘數並驗證輸出是否正確。

更新於:2021-05-31

7K+ 瀏覽

開啟你的職業生涯

完成課程並獲得認證

開始
廣告
© . All rights reserved.