我使用以下代码块来测试gw-basic
程序中的某些条件。
IF Average >= 80 AND Average <= 100 THEN Grade$ = "A"
IF Average >= 70 AND Average <= 79 THEN Grade$ = "B"
IF Average >= 60 AND Average <= 69 THEN Grade$ = "C"
IF Average >= 50 AND Average <= 59 THEN Grade$ = "D"
IF Average >= 40 AND Average <= 49 THEN Grade$ = "E" ELSE Grade$ = "F"
PRINT "Average is: ", Average
PRINT "Grade is: ", Grade$
它总是打印Grade is: F
Àverage的值。
这个gw-basic程序有什么错误?
答案 0 :(得分:4)
IF Average >= 40 AND Average <= 49 THEN Grade$ = "E" ELSE Grade$ = "F"
由于此IF THEN ELSE
行始终会为 Grade $ 分配一个值(无论平均值的内容),所有以前的IF THEN
这不算多少。
然而,解决方案非常非常简单。不要使用ELSE
子句并将 Grade $ 设置为默认值“F”。这样,只有当所有这些条件评估为false时,程序才会返回“Grade is:F”。
Grade$ = "F"
IF Average >= 80 AND Average <= 100 THEN Grade$ = "A"
IF Average >= 70 AND Average <= 79 THEN Grade$ = "B"
IF Average >= 60 AND Average <= 69 THEN Grade$ = "C"
IF Average >= 50 AND Average <= 59 THEN Grade$ = "D"
IF Average >= 40 AND Average <= 49 THEN Grade$ = "E"
PRINT "Average is: ", Average
PRINT "Grade is: ", Grade$
答案 1 :(得分:0)
假设打印平均线为行号70,因此在除ELSE行之外的每一行之后写入GOTO 70.尝试是否有效。