|  | 
 
| 
不太理解这个报错,请大佬们指教,应该如何修改呢?我看和老师视频讲的代码一样呀
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  
 程序如下:
 #include <stdio.h>
 int main()
 {
 char i;
 
 printf("请输入分数:");
 scanf("%c",&i);
 
 switch (i)
 {
 case "A": printf("90分及以上\n");break;   \\此处为11行
 case "B": printf("80-90\n");break;
 
 default: printf("80以下\n");break;
 }
 
 return 0;
 }
 
 报错:
 this constant expression has type "const char *" instead of the required "int" type [ln11,col14]
 invalid conversion from 'const char*' to 'int' [-fpermissive] gcc [ln11,col14]
 this constant expression has type "const char *" instead of the required "int" type [ln12,col14]
 
问题出在第11行的switch语句中,你使用了双引号来表示"A"和"B",但是在switch语句中,应该使用单引号来表示字符常量。双引号表示字符串常量。
 正确的代码如下:
 
 
 复制代码#include <stdio.h>
int main()
{
    char i;
    printf("请输入分数:");
    scanf("%c",&i);
    switch (i)
    {
        case 'A': printf("90分及以上\n");break;
        case 'B': printf("80-90\n");break;
        default: printf("80以下\n");break;
    }
    return 0;    
}
 通过将双引号改为单引号,你就可以正确地使用字符常量来进行switch语句的比较了。
 | 
 |