嵌套IF ...然后......在GW-BASIC中使用

时间:2016-09-28 18:48:18

标签: if-statement nested gw-basic

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

2 个答案:

答案 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.尝试是否有效。