QBasic / QB64:如何清除IF,如果“阶梯”?

时间:2018-03-29 06:09:43

标签: qbasic qb64

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

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

START      END  
123        150  
489        552  
590        600  

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

6 个答案:

答案 0 :(得分:2)

如果将所有比较放在由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

答案 1 :(得分: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更改为9可能是有益的DIM 1}} line。
  3. 它使比较短路,这意味着如果第一个条件为假,它会停止检查并说它不是一个Lo Shu Square,类似于IF-THEN-ELSE语句塔(每个ELSE都是PRINT "This is not a Lo Shu square.")QB64的AND运算符计算两个操作数,即使第一个操作数为0或另一个“false”值。这可能会快得多,但在这种情况下您可能不会注意到差异。
  4. 另一方面,它确实有一些缺点:

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

答案 2 :(得分: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

答案 3 :(得分: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)

答案 4 :(得分: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())

答案 5 :(得分: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