问题代码:
#include <stdio.h>
void main()
{
int score;
printf("请输入成绩(0-100):");
scanf("%d",&score);
if(score<0)
printf("输入数据错误\n");
else if(score<60)
putchar('E');
putchar('\n');
else if(score<70)
printf("D\n");
else if(score<80)
printf("C\n");
else if(score<90)
printf("B\n");
else if(score <= 100)
printf("A\n");
else
printf("输入数据错误\n");
}
编译后结果:
翻译错误提示:else没有匹配的if,即else没有if与之相呼应。
修改后代码:
#include <stdio.h>
void main()
{
int score;
printf("请输入成绩(0-100):");
scanf("%d",&score);
if(score<0)
printf("输入数据错误\n");
else if(score<60)
{
putchar('E');
putchar('\n');
}
else if(score<70)
printf("D\n");
else if(score<80)
printf("C\n");
else if(score<90)
printf("B\n");
else if(score <= 100)
printf("A\n");
else
printf("输入数据错误\n");
}
对比两个代码会发现问题所在了。