检查 Imagine Logo 中的变量 if 是偶数还是奇数

Check if a variable if is even or odd in Imagine Logo

我需要检查一个变量是偶数还是奇数,但我不知道如何在 Imagine Logo 中进行。

如果不可能,我至少需要这样的东西...

if (a = 1 || a = 2 || a = 3)

通常这是通过使用除法后求余数的方法来完成的。偶数除以 2 的余数为 0。奇数除以 2 的余数为 1。如果您的数字为负数,则可能需要注意如何返回余数。

我不太了解 Imagine Logo,但是这个 online interpreter "a Logo"有三种说法("Reference"下link):

remainder expr expr
expr % expr
modulo expr expr

Outputs the remainder (modulus). For remainder and % the result has the same sign as the first input; for modulo the result has the same sign as a the second input.

假设你有这些可用的,那么如果你想测试负数是奇数还是偶数,如果你使用 remainder% 而不是 modulo 会更容易.所以偶数:

(a % 2) = 0
(remainder a 2) = 0

对于奇数:

(a % 2) = 1
(remainder a 2) = 1

您可能想也可能不想查看 "Modulo operation" 的维基百科页面。