3

我使用以下代码块来测试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 程序有什么错误?

4

2 回答 2

4
IF Average >= 40 AND Average <= 49 THEN Grade$ = "E" ELSE Grade$ = "F"  

因为这一行将始终为Grade$IF THEN ELSE赋值(无论Average的内容如何),所有前面的 ' 都不算什么。 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$
于 2016-12-12T19:07:56.530 回答
0

假设打印平均行是第 70 行,所以在除 ELSE 行之外的每一行之后写 GOTO 70。试试这是否有效。

于 2017-08-28T14:52:53.443 回答