1

不确定我是否在这里使用了正确的术语,但无论出于何种原因,QBasic 不理解“x = y = z”的内容。它仅限于两个。

为了解决这个问题,我这样做了:

IF sum(1) = sum(2) THEN
    IF sum(2) = sum(3) THEN
        IF sum(3) = sum2(1) THEN
            IF sum2(1) = sum2(2) THEN
                IF sum2(2) = sum2(3) THEN
                    IF sum2(3) = sum3 THEN
                        IF sum3 = sum4 THEN
                            PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
                        ELSE
                            PRINT "This is not a Lo Shu Square."
                        END IF
                    END IF
                END IF
            END IF
        END IF
    END IF
END IF
END

绝对有效,但有件事告诉我有一种更简单的方法可以让它检查所有的总和是否相同。有什么建议么?

4

6 回答 6

3

如果将所有比较放在由 AND 分隔的一行上,它将起作用,如下所示:

REM code to shrink IFTHEN ladder:
IF sum(1) = sum(2) AND sum(2) = sum(3) AND sum(3) = sum2(1) AND sum2(1) = sum2(2) AND sum2(2) = sum2(3) AND sum2(3) = sum3 AND sum3 = sum4 THEN
    PRINT "This is a Lo Shu Square, with all sums equaling"; sum(1)
ELSE
    PRINT "This is not a Lo Shu Square."
END IF
END
于 2018-03-29T23:49:40.360 回答
1

检查循环的更简单方法:

testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
    IF testvals(i) <> testvals(i + 1) THEN
        PRINT "This is not a Lo Shu square."
        END
    END IF
NEXT
PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
于 2018-04-09T22:55:57.790 回答
1

将逻辑合并为一个函数:

FUNCTION isLoShuSquare (sums() AS DOUBLE)

    isLoShuSquare = 1

    DIM i AS INTEGER
    FOR i = 0 TO UBOUND(sums) - 1
        IF sums(i) <> sums(i + 1) THEN
            isLoShuSquare = 0
            EXIT FOR
        END IF
    NEXT i

END FUNCTION

然后加载数组并将其传递给函数:

DIM sums(7) AS DOUBLE
DIM i AS INTEGER
i = 0
sums(i) = sum(1): i = i + 1
sums(i) = sum(2): i = i + 1
sums(i) = sum(3): i = i + 1
sums(i) = sum2(1): i = i + 1
sums(i) = sum2(2): i = i + 1
sums(i) = sum2(3): i = i + 1
sums(i) = sum3: i = i + 1
sums(i) = sum4

PRINT isLoShuSquare(sums())
于 2018-04-10T14:56:17.997 回答
1

您还可以将逻辑编码为循环:

DIM testvals(8)
testvals(0) = sum(1)
testvals(1) = sum(2)
testvals(2) = sum(3)
testvals(3) = sum2(1)
testvals(4) = sum2(2)
testvals(5) = sum2(3)
testvals(6) = sum3
testvals(7) = sum4
DO 
    FOR i = 1+LBOUND(testvals) TO UBOUND(testvals)
        IF testvals(i-1) <> testvals(i) THEN
            PRINT "This is not a Lo Shu square."
            EXIT DO
        END IF
    NEXT
    PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
LOOP WHILE 1 = 0

这有几个好处:

  1. 如果您更改代码,则更容易发现拼写错误。
  2. 在类似情况下,您始终可以添加和删除测试值。在这种情况下,不需要做这样的事情,但在其他一些情况下,只需在行中键入testvals(8) = value并将其更改8为 a可能会有所帮助。9DIM
  3. 它使比较短路,这意味着如果第一个条件为假,它会停止检查并说它不是 Lo Shu Square,类似于IF-THEN-ELSE语句塔(其中每个ELSE都是PRINT "This is not a Lo Shu square.")QB64 的AND运算符评估两个操作数,即使第一个操作数是0 或另一个“假”值。这可能会快得多,尽管在这种情况下您可能不会注意到差异。

另一方面,它确实有一些缺点:

  1. 在这种情况下不使用 QB64 是一种不寻常的模式AND。事实上,这是存在的一个很好的理由AND
  2. 您可以轻松删除组合使用AND的测试值,而无需重新编号testvals数组中的项目或更改其维度。
  3. 即使您有许多测试值,通常最好编写一个IF a AND b AND c AND ... THEN ... END IF自己生成块的小程序(或类似于您的IF-THEN塔的东西以保留短路行为)并将输出粘贴到需要的程序代码中。
于 2018-03-31T01:03:21.863 回答
1

检查数组的更简单方法:

testvals(1) = sum(1)
testvals(2) = sum(2)
testvals(3) = sum(3)
testvals(4) = sum2(1)
testvals(5) = sum2(2)
testvals(6) = sum2(3)
testvals(7) = sum3
testvals(8) = sum4
FOR i = 1 TO 7
    IF testvals(i) <> testvals(i + 1) THEN
        f = -1
        EXIT FOR
    END IF
NEXT
IF f THEN
    PRINT "This is not a Lo Shu square."
ELSE
    PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
于 2018-03-31T06:43:16.113 回答
0

在函数的循环中检查数组的另一种方法:

DIM sums(8) AS DOUBLE
sums(1) = sum(1)
sums(2) = sum(2)
sums(3) = sum(3)
sums(4) = sum2(1)
sums(5) = sum2(2)
sums(6) = sum2(3)
sums(7) = sum3
sums(8) = sum4
IF isLoShuSquare(sums()) = 0 THEN
    PRINT "This is not a Lo Shu square."
ELSE
    PRINT "This is a Lo Shu square, with all sums equaling"; sum(1)
END IF
END
FUNCTION isLoShuSquare (sums() AS DOUBLE)
isLoShuSquare = -1
FOR i = 1 TO UBOUND(sums) - 1
    IF sums(i) <> sums(i + 1) THEN
        isLoShuSquare = 0
        EXIT FUNCTION
    END IF
NEXT
END FUNCTION
于 2018-04-13T01:29:09.017 回答