c# - 如果 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
}
解决方案
如果您提供了您所面临的具体情况,这将有所帮助;但我认为您要问的是...如何让我的程序在循环中执行某些操作;但不是每次,而是每n次,其中n是常数?
如果是这种情况,那么您需要使用模块化算术。我不得不假设/想象循环(这不是问题;但应该是)给出这个例子......
for (int firstInt = 0; firstInt < 100; firstInt++)
{
const int secondInt = 10;
if((firstInt % secondInt) == 0)
{
// "call the method"
}
}
基本上这就是说:如果该firstInt
值完全可以被 整除secondInt
,那么它将调用该方法。
您可能想了解模数运算符(又名余数运算符)。
推荐阅读
- javascript - VueJs @error 用于处理图像中的断开链接
- javascript - Bootstrap Modal gets disappeared immidiately
- jquery - 输入更改时有条件地在按钮上显示文本
- java - 如何将文本发送到在 selenium webdriver 中使用自动完成功能的搜索框
- html - 使用 javascript 检查浏览器中对 shadow dom v1 的支持
- python - 使用 Boto3 从 AWS S3 读取 gzip 文件的内容
- spring-boot - 使用 Spring Boot RestTemplate-Client 提交 Json 到远程 SpringBoot 微服务
- android - 使用 MediaRecorder 录制的音频无法使用 MediaPlayer 播放
- android - 如何在android中完全禁用自动填充
- java - Java lambda 只抛出表达式样式而不是语句样式