-1

我用于计算折扣的 QBasic 代码如下:

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 行)出现错误,但在程序中找不到任何语法错误。

4

4 回答 4

3

QBasic 中正确的语法是ELSEIF代替ELSE IF(注意这两个词写成一个,没有空格)。

于 2013-08-09T08:23:04.873 回答
2

以下是没有任何 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
于 2014-10-26T18:27:28.490 回答
2

您应该将 ELSE IF 替换为 ELSEIF。

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

或者,如果您真的想按照标题的建议制作“LADDER”,您应该:

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
于 2013-08-09T08:25:29.017 回答
0

这通过使折扣和金额更具可读性来解决阶梯问题。

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
于 2016-08-07T23:27:10.793 回答