gpt4 book ai didi

if-statement - GW-BASIC 中的嵌套 IF...THEN...ELSE

转载 作者:行者123 更新时间:2023-12-01 12:25:53 25 4
gpt4 key购买 nike

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

最佳答案

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

因为这行 IF THEN ELSE 总是会为 Grade$ 赋值(不管 Ave​​rage 的内容如何),所有的前面的 IF THEN 没有多大意义。

然而,解决方案非常非常简单。不要使用 ELSE 子句并为 Grade$ 提供默认值“F”作为开头。这样,只有当所有这些条件的计算结果都为假时,程序才会返回“成绩为: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$

关于if-statement - GW-BASIC 中的嵌套 IF...THEN...ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39755438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com