C#如何确定一个数字是否为另一个的倍数? [英] C# How to determine if a number is a multiple of another?
本文介绍了C#如何确定一个数字是否为另一个的倍数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不使用字符串处理(检查是否出现.
或,
字符),而是将int计算的结果强制转换为字符串.
Without using string manipulation (checking for an occurrence of the .
or ,
character) by casting the product of an int calculation to string.
和
不使用try/catch方案,而是依靠数据类型的错误.
without using try / catch scenarios relying on errors from data types.
如果数字是另一个的倍数,您如何具体使用C#进行检查?
How do you specifically check using C# if a number is a multiple of another?
例如6是3的倍数,而7不是.
For example 6 is a multiple of 3, but 7 is not.
推荐答案
尝试
public bool IsDivisible(int x, int n)
{
return (x % n) == 0;
}
模运算符%返回x除以n后的余数,如果x被n整除,它将始终为0.
The modulus operator % returns the remainder after dividing x by n which will always be 0 if x is divisible by n.
有关更多信息,请参见 MSDN上的%运算符.
For more information, see the % operator on MSDN.
这篇关于C#如何确定一个数字是否为另一个的倍数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文