java num%2 在java中是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34247595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:39:10  来源:igfitidea点击:

what does num%2 mean in java?

javaloopsconditionalmodulus

提问by studentS

this is the code fragment. I'm not too sure what num%2 is though. % would give the remainder, so does that mean that the remainder of num must be 2?

这是代码片段。我不太确定 num%2 是什么。% 会给出余数,那么这是否意味着 num 的余数必须是 2?

int num = 1, max = 20;
while (num < max)
{
     if (num%2 == 0)
          System.out.println(num);
     num++;
}

回答by JRowan

num%2==0

means the remainder of num divided by two which if something is divided by two the only remainder it could have is either 0 or 1, so its taking the remainder of dividing num by 2 and checking if it is equal to 0

表示 num 除以 2 的余数,如果某物被二除,则其唯一的余数是 0 或 1,因此它取 num 除以 2 的余数并检查它是否等于 0