Qbasic-尝试调用一个子,但是我收到一条消息,内容为“预期的呼叫,子名(...)”

时间:2018-08-24 18:33:29

标签: arrays subroutine qbasic

这是代码,它包含5个名称并将它们按字母顺序排列。我有一个对名称进行排序的子,但显然我没有以正确的方式调用它。

DIM i AS INTEGER
DIM q AS INTEGER
DIM a AS STRING
DIM n(5) AS STRING
DIM z AS INTEGER
DIM x AS INTEGER
DIM y AS INTEGER
DIM g AS INTEGER

start:
CLS

FOR i = 1 TO 5
    INPUT "Name:", n(i) 'receiving 5 names
NEXT i

call Sort()                                **This is the line with the error**

PRINT "Names in Alphabetical Order:"
FOR i = 1 TO 5
    PRINT n(i)
NEXT i
INPUT "Again(y, n)", a
IF LCASE$(a) = "y" THEN GOTO start
IF LCASE$(a) <> "y" THEN STOP

'end of main program

SUB Sort (`enter code here`)
    FOR i = 1 TO 5
        FOR q = 1 TO 4
        IF LCASE$(n(q)) > LCASE$(n(q + 1)) THEN SWAP n(q), n(q + 1)
        IF LCASE$(n(q)) = LCASE$(n(q + 1)) THEN

            z = LEN(n(q))
            x = LEN(n(q + 1))
            IF z > x THEN g = x
            IF z < x THEN g = z

            IF z > 1 AND x > 1 THEN
                FOR y = 2 TO g
                    IF LCASE$(MID$(n(q), y, 1)) > LCASE$(MID$(n(q + 1), y, 1)) THEN
                        SWAP n(q), n(q + 1)
                        END
                    END IF
                    IF y = g THEN SWAP n(q), n(q + 1)
                NEXT y
            END IF
        END IF
    NEXT q
NEXT i

END SUB

我尝试将参数放在子数组“ SUB Sort(n())”中,该数组中包含名称,但它表示数组类型错误。

2 个答案:

答案 0 :(得分:2)

您的问题是您在没有参数的情况下键入了SORT()

首先,在QB中使用正确的语法调用SUBFUNCTION可能有点奇怪,而CALL关键字的语法使事情更加混乱:

' No arguments means you MUST NOT include the () afterward.
CALL SubNameE

' One non-array argument: n
CALL SubNameX(n)

' Two non-array arguments: a, b
CALL SubNameY(a, b)

' An array argument and a non-array argument: arr(), v
CALL SubNameZ(arr(), v)

注意如何使用SUB将数组传递给FUNCTIONname()过程。当您编写call SORT()时,解释器可能会感到困惑,因为SORT()是数组的语法,而不是没有参数的SUB / FUNCTION调用。

如果有帮助,您也可以不使用CALL关键字来编写上述代码行:

' No arguments means only the procedure name is listed.
SubNameE

' One non-array argument: n
SubNameX n

' Two non-array arguments: a, b
SubNameY a, b

' An array argument and a non-array argument: arr(), v
SubNameZ arr(), v

请注意,要让SUBFUNCTION接受数组参数,必须声明它们,如下所示:

SUB SORT (n() AS STRING)
' or if you wanted to use a type suffix
SUB SORT (n$())

答案 1 :(得分:2)

这是将数组传递到qb64中的sort子例程的简单方法:

DIM n(5) AS STRING
n(1) = "zachary smith"
n(2) = "xerox johnson"
n(3) = "yackle jones"
n(4) = "arnold aglet"
n(5) = "beth anderson"
CALL Sort(n())
FOR i = 1 TO 5: PRINT n(i): NEXT
END
SUB Sort (n() AS STRING)
FOR i = 1 TO 5
    FOR j = i + 1 TO 5
        IF n(i) > n(j) THEN SWAP n(i), n(j)
    NEXT
NEXT
END SUB

这是一种无需指定参数即可调用sort子例程的简便方法:

DIM SHARED n(5) AS STRING
n(1) = "zachary smith"
n(2) = "xerox johnson"
n(3) = "yackle jones"
n(4) = "arnold aglet"
n(5) = "beth anderson"
CALL Sort
FOR i = 1 TO 5: PRINT n(i): NEXT
END
SUB Sort
FOR i = 1 TO 5
    FOR j = i + 1 TO 5
        IF n(i) > n(j) THEN SWAP n(i), n(j)
    NEXT
NEXT
END SUB