检查 2 个数是否为倍数
Checking If 2 Numbers are Multiples
我需要一种使用 VB 脚本检查两个不同数字是否互为倍数的方法。所以 2 和 4 会 return 是或肯定,但 2 和 5 会 return 否或否定。
你可以直接使用 Mod
:
If int1 Mod int2 = 0 Then
WScript.Echo int1 & " is a multiple of " & int2
End If
编辑:
如果你想测试其中一个是否是另一个的倍数:
If int1 Mod int2 = 0 Then
WScript.Echo int1 & " is a multiple of " & int2
ElseIf int2 Mod int1 = 0 Then
WScript.Echo int2 & " is a multiple of " & int1
Else
WScript.Echo "Neither " & int1 & " nor " & int2 & " is a multiple of the other."
End If
编辑 2:
根据下面@Ansgar 的建议,如果您只需要知道一个是否是另一个的倍数但又不想知道是哪个,这里有一个简单 returns 布尔值的函数:
Function TestMultiple(int1, int2)
TestMultiple = (int1 Mod int2 = 0) Or (int2 Mod int1 = 0)
End Function
我需要一种使用 VB 脚本检查两个不同数字是否互为倍数的方法。所以 2 和 4 会 return 是或肯定,但 2 和 5 会 return 否或否定。
你可以直接使用 Mod
:
If int1 Mod int2 = 0 Then
WScript.Echo int1 & " is a multiple of " & int2
End If
编辑:
如果你想测试其中一个是否是另一个的倍数:
If int1 Mod int2 = 0 Then
WScript.Echo int1 & " is a multiple of " & int2
ElseIf int2 Mod int1 = 0 Then
WScript.Echo int2 & " is a multiple of " & int1
Else
WScript.Echo "Neither " & int1 & " nor " & int2 & " is a multiple of the other."
End If
编辑 2:
根据下面@Ansgar 的建议,如果您只需要知道一个是否是另一个的倍数但又不想知道是哪个,这里有一个简单 returns 布尔值的函数:
Function TestMultiple(int1, int2)
TestMultiple = (int1 Mod int2 = 0) Or (int2 Mod int1 = 0)
End Function