c语言 - 收到错误:"variable cannot appear in a constant-expression"



我已经在c-free5.0中制作了一个基本代码,如果与中的相同,如果语句都显示不错。但是当我使用开关时,它会出现错误:" per"不能以恒定表达出现,为什么会给出错误?

#include<stdio.h>
#include<conio.h>
int main()
{
    int urdu=134;
    int eng=112;
    int isl=72;
    int ps=58;
    int maths=137;
    int phy=128;
    int chem=120;
    int bio=115;
    int total=(urdu+eng+isl+ps+maths+phy+chem+bio);
    int per=(total*100)/1050;
    printf("Urdu=               %dn", urdu);
    printf("English=            %dn", eng);
    printf("Islmiyat=           %dn", isl);
    printf("Pakistan Studies=   %dn", ps);
    printf("Mathematics=        %dn", maths);
    printf("Physics=            %dn", phy);
    printf("Chemistery=         %dn", chem);
    printf("Biology=            %dnn", bio);
    printf("Percentage:         %dn",per);
    switch(per)
    {
        case (per>80):
        printf("A+");
        case (per>70):
        printf("A");
    }
    getch();
}

一个开关案例是一系列IF检查一系列变量的 equality 的简短手。您不能以指定的方式使用它。相反,您可以只使用普通的旧 if语句:

if (per > 80) {
    printf("A+");
else if (per > 70) {
    printf("A");
}

相关内容

最新更新