这是代码,它需要 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 中放置一个参数"SUB Sort(n(((",其中包含名称的数组,但它说数组类型不正确。
你的问题是你在没有参数的情况下输入了SORT()
。
一开始,在 QB 中,使用正确的语法调用SUB
或FUNCTION
可能有点奇怪,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)
请注意数组如何使用name()
传递给SUB
和FUNCTION
过程。 当您编写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
请注意,要使SUB
或FUNCTION
接受数组参数,它必须声明它们,如下所示:
SUB SORT (n() AS STRING)
' or if you wanted to use a type suffix
SUB SORT (n$())
以下是将数组传递给 qb64 中的排序子例程的简单方法:
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
下面是在不指定参数的情况下调用排序子例程的更简单方法:
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