我计算折扣的Q基本代码如下:
CLS
INPUT "ENTER BILL AMOUNT";AMT
IF AMT <= 6000 THEN
DISCOUNT = 0
ELSEIF AMT > 6000 AND AMT <= 15000 THEN
DISCOUNT = 0.2 * AMT
ELSEIF AMT > 15000 AND AMT <= 50000 THEN
DISCOUNT = 0.3 * AMT
ELSEIF AMT > 50000 AND AMT <= 100000 THEN
DISCOUNT = 0.4 * AMT
ELSEIF AMT > 100000 AND AMT<= 200000 THEN
DISCOUNT = 0.5 * AMT
ELSEIF AMT > 200000 AND AMT <= 1000000 THEN
DISCOUNT = 0.6 * AMT
ELSE
DISCOUNT = 0.7 * AMT
END IF
NET = AMT - DISCOUNT
PRINT "THE BILL AMOUNT IS:"; NET
END
然而,在运行程序时,我在第50行(程序的第5行)遇到了一个错误,尽管我在程序中找不到任何语法错误。
QBasic中的正确语法是ELSEIF
,而不是ELSE IF
(请注意,这两个单词是一体写的,没有空格)。
下面是一个没有任何ELSE的解决方案:
CLS
INPUT "ENTER BILL AMOUNT BEFORE DISCOUNT"; AMT
DISCOUNT = 0.7
IF AMT <= 1000000 THEN DISCOUNT = 0.6
IF AMT <= 200000 THEN DISCOUNT = 0.5
IF AMT <= 100000 THEN DISCOUNT = 0.4
IF AMT <= 50000 THEN DISCOUNT = 0.3
IF AMT <= 15000 THEN DISCOUNT = 0.2
IF AMT <= 6000 THEN DISCOUNT = 0
DISCOUNT = DISCOUNT * AMT
NET = AMT - DISCOUNT
PRINT "THE BILL AMOUNT IS:"; NET
END
您应该用ELSEIF替换ELSE IF。
CLS
INPUT "ENTER BILL AMOUNT"; AMT
IF AMT <= 6000 THEN
DISCOUNT = 0
ELSEIF AMT > 6000 AND AMT <= 15000 THEN
DISCOUNT = 0.2 * AMT
ELSEIF AMT > 15000 AND AMT <= 50000 THEN
DISCOUNT = 0.3 * AMT
ELSEIF AMT > 50000 AND AMT <= 100000 THEN
DISCOUNT = 0.4 * AMT
ELSEIF AMT >100000 AND AMT <= 200000 THEN
DISCOUNT = 0.5 * AMT
ELSEIF AMT > 200000 AND AMT <= 1000000 THEN
DISCOUNT = 0.6 * AMT
ELSE
DISCOUNT = 0.7 * AMT
END IF
NET = AMT - DISCOUNT
PRINT "THE BILL AMOUNT IS:" NET
END
或者,如果你真的想按照你的标题做一个"梯子",你应该:
CLS
INPUT "ENTER BILL AMOUNT";AMT
IF AMT <=6000 THEN
DISCOUNT = 0
ELSE
IF AMT > 6000 AND AMT <= 15000 THEN
DISCOUNT =0.2 * AMT
ELSE
IF AMT > 15000 AND AMT <= 50000 THEN
DISCOUNT =0.3*AMT
ELSE
IF AMT >50000 AND AMT<=100000 THEN
DISCOUNT =0.4*AMT
ELSE
IF AMT >100000 AND AMT<=200000 THEN
DISCOUNT =0.5*AMT
ELSE
IF AMT >200000 AND AMT<=1000000 THEN
DISCOUNT =0.6*AMT
ELSE
DISCOUNT =0.7*AMT
END IF
END IF
END IF
END IF
END IF
END IF
NET=AMT - DISCOUNT
PRINT "THE BILL AMOUNT IS:"NET
END
这通过使折扣和金额更具可读性来解决阶梯问题。
CLS
INPUT "ENTER BILL AMOUNT"; AMT
SELECT CASE AMT
CASE IS <= 6000
DISCOUNT = 0
CASE 6000 TO 15000
DISCOUNT = 0.2 * AMT
CASE 15000 TO 50000
DISCOUNT = 0.3 * AMT
CASE 50000 TO 100000
DISCOUNT = 0.4 * AMT
CASE 100000 TO 200000
DISCOUNT = 0.5 * AMT
CASE 200000 TO 1000000
DISCOUNT = 0.6 * AMT
CASE ELSE
DISCOUNT = 0.7 * AMT
END SELECT
NET = AMT - DISCOUNT
PRINT "THE BILL AMOUNT IS:"; NET
END