我正在编写一个qb64/QBasic软件程序,将0-100的数字等级转换为字母等级(a、b、c、d、f(。我设置了它,以便使用字符串/数组来输入无限的等级。然而,当我试图计算字母等级并调用它以用CCD_ 1打印时;函数所需的数目";有人能帮我吗?
ReDim MyList(0) As String
Dim Grade
Dim Index%
_Title "Letter Grade Calculator"
Print "Enter your numerical grades. To finish enter (exit)"
Do While Grade$ <> "exit"
Input "Enter Grade"; Grade$
If Grade$ <> "exit" Then
Index% = Index% + 1
ReDim _Preserve MyList(Index%)
MyList(Index%) = Grade$
End If
Loop
Cls
For Index% = 1 To UBound(MyList)
Print MyList(Index%);
Print "Letter Grade:"; calcGrade!(MyList(Index%))
Next Index%
Function calcGrade! (numGrade)
Select Case val(numGrade)
Case is > 100
calcGrade! = "A+"
Case Is > 90
calcGrade! = "A"
Case Is > 80
calcGrade! = "B"
Case Is > 70
calcGrade! = "C"
Case Is > 60
calcGrade! = "D"
Case Is > 50
calcGrade! = "F"
Case Else
calcGrade! = "ERROR: Invalid character"
End Select
End Function
然而,当我尝试计算字母等级并调用它以使用
calcGrade!(MyList(Index%))
打印时,我收到错误";函数所需的数目";
错误";函数所需的数目";您为calcGrade!(MyList(Index%))
收到的意味着calcGrade函数希望从您那里收到一个数值参数,该参数将被分配给其numGrade参数。
Function calcGrade! (numGrade)
在没有任何显式声明的情况下,numGrade参数默认为单个数字格式。但是,您通过MyList提供字符串参数。
另一个错误是,函数名称上的感叹号声明函数返回单个值。因此,您不能将所有这些";A+"A"B"C"D"F";,以及";错误:无效字符">字符串值。
解决方案是在calcGrade$和numGrade$上使用$字符串后缀。
...
Cls
For Index% = 1 To UBound(MyList)
Print MyList(Index%); " -> ";
Print "Letter Grade:"; calcGrade$(MyList(Index%))
Next Index%
Function calcGrade$ (numGrade$)
Select Case val(numGrade$)
Case Is > 100
calcGrade$ = "A+"
Case Is > 90
calcGrade$ = "A"
Case Is > 80
calcGrade$ = "B"
Case Is > 70
calcGrade$ = "C"
Case Is > 60
calcGrade$ = "D"
Case Is > 50
calcGrade$ = "F"
Case Else
calcGrade$ = "ERROR: Invalid character"
End Select