Java,检查整数是否是数字的倍数 [英] Java, Check if integer is multiple of a number

查看:1635
本文介绍了Java,检查整数是否是数字的倍数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查Java整数是否是另一个数字的倍数?例如,如果 int j 是4的倍数。

How do I check if a Java integer is a multiple of another number? For example, if int j is a multiple of 4.

推荐答案

使用余数运算符(也称为模运算符)返回除法的余数并检查它是否为零:

Use the remainder operator (also known as the modulo operator) which returns the remainder of the division and check if it is zero:

if (j % 4 == 0) {
     // j is an exact multiple of 4
}

这篇关于Java,检查整数是否是数字的倍数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆