首页 > 解决方案 > 如果 int 值是特定值的倍数,则运算符调用方法

问题描述

如果 int 是循环中特定值(例如 10、20、30 等)的倍数,当我想调用方法时,我需要帮助来了解什么是正确的运算符。

前任。

int firstInt = 0;
int secondInt = 10;
for (int firstInt = 0; firstInt < 100; firstInt++)
{
    if(firstInt "value has increased by secondInt") {
        "call the method"
    }
    // do something else for each iteration
}

标签: c#

解决方案


如果您提供了您所面临的具体情况,这将有所帮助;但我认为您要问的是...如何让我的程序在循环中执行某些操作;但不是每次,而是每n次,其中n是常数?

如果是这种情况,那么您需要使用模块化算术。我不得不假设/想象循环(这不是问题;但应该是)给出这个例子......

for (int firstInt = 0; firstInt < 100; firstInt++)
{
   const int secondInt = 10;

   if((firstInt % secondInt) == 0)
   {
       // "call the method"
   }
}

基本上这就是说:如果该firstInt值完全可以被 整除secondInt,那么它将调用该方法。

您可能想了解模数运算符(又名余数运算符)


推荐阅读