如果QBasic中的其他梯子

时间:2013-08-09 08:20:20

标签: if-statement qbasic

我的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 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:2)

您应该用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

或者,如果您真的想在标题中提出“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

答案 2 :(得分: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

答案 3 :(得分: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