关于##else if语句##

咋回事?为啥加;,加了也报错

img


#include
#define   RATE1 0.13230
#define   RATE2 0.15040
#define   RATE3 0.30025
#define   RATE4 0.34025

#define   BREAK1 360.0
#define   BREAK2 468.0
#define   BREAK3 720.0

#define   BASE1  (RATE1*BREAK1)
#define   BASE2  (BASE1)+(RATE2*(BREAK2-BREAK1)))
#define   BASE3  BASE1+BASE2+(RATE3*(BREAK3-BREAK2))
int main(void)
{
    double kwh;
    double bill;

    printf("please enter the kwh used.\n");
    scanf("%f", &kwh);
    if (kwh <= BREAK1)
        bill = RATE1 * kwh;

    else
    {
        if (kwh <= BREAK2)
            bill = BASE1 + (RATE2 * (kwh - BREAK1));


        else
        {
            if (kwh <= BREAK3)
                bill = BASE2 + (RATE3 * (kwh - BREAK2));

            else
                bill = BASE3 + (RATE4 * (kwh - BREAK3));
        }
    }
    printf("%.1f is %.1f\n", kwh, bill);
    return 0;
}



修改处见注释,供参考:

#include <stdio.h>
#define   RATE1 0.13230
#define   RATE2 0.15040
#define   RATE3 0.30025
#define   RATE4 0.34025

#define   BREAK1 360.0
#define   BREAK2 468.0
#define   BREAK3 720.0

#define   BASE1  (RATE1*BREAK1)
#define   BASE2  ((BASE1)+(RATE2*(BREAK2-BREAK1))) //修改
#define   BASE3  BASE1+BASE2+(RATE3*(BREAK3-BREAK2))
int main(void)
{
    double kwh;
    double bill;

    printf("please enter the kwh used.\n");
    scanf("%lf", &kwh); //scanf("%f", &kwh); //修改
    if (kwh <= BREAK1)
        bill = RATE1 * kwh;

    else
    {
        if (kwh <= BREAK2)
            bill = BASE1 + (RATE2 * (kwh - BREAK1));
        else
        {
            if (kwh <= BREAK3)
                bill = BASE2 + (RATE3 * (kwh - BREAK2));

            else
                bill = BASE3 + (RATE4 * (kwh - BREAK3));
        }
    }
    printf("%.1f is %.1f\n", kwh, bill);
    return 0;
}

我加了半个括号就不错了
但跑出来的是垃圾值

img

#define BASE2 后面的代码多了一个右括号了


可以看下c语言参考手册中的 c语言-语句