bmu*_*ler 2 iphone objective-c ios
我如何确定一个数字是否是5的倍数?
在我的应用程序,我想一个方法来运行,如果一定数目不是5的倍数,而另一种方法来运行,如果该方法是 5的倍数.
谢谢你的帮助!
Jon*_*art 16
使用模运算符检查整数除法的余数.
let num = 75
if num.isMultiple(of: 5) {
// multiple of 5
} else {
// not a multiple of 5
}
Run Code Online (Sandbox Code Playgroud)
使用模数运算符:
if (num % 5 == 0)
//the number is a multiple of 5.
else
// the number is not a multiple of 5.
Run Code Online (Sandbox Code Playgroud)
模数运算符返回除法的余数而不是除法本身,因此该逻辑将适用于任何数字,而不仅仅是5 if (num % 3 == 0) //multiple of 3
| 归档时间: |
|
| 查看次数: |
1889 次 |
| 最近记录: |