#很简单的问题 输入成绩输出对应的等级,为什么我在输入完成绩后按回车键一直没反应的 一直可以不停的回车?
#include <stdio.h>
int main() {
float score;
printf("Please enter the score\n");
scanf("%f\n",&score);
int c=score/10;
switch (c) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Grades belong to E");break;
case 6:
printf("Grades belong to D");break;
case 7:
printf("Grades belong to C");break;
case 8:
printf("Grades belong to B");break;
case 9:
printf("Grades belong to A");break;
}
return 0;
}
scanf语句中不能有‘\n',改成scanf("%f", &score)。当使用 scanf("%d\n", &a) 时,它会等待用户输入一个整数,并在输入结束后将其存储在变量 a 中。在这里,%d 是一个格式说明符,表示要读取的 数据类型 是整数。然而,如果用户在输入整数之后输入了换行符或空格等空白字符,scanf 函数会停止读取,并将其解释为输入的结束。说明他必须读取第一个换行,再读取结束的换行.
scanf()里面去掉 \n 试试
把换行删了就行
scanf("%f\n",&score); 这里多了 '\n' 符,应为:scanf("%f",&score);
【以下回答由 GPT 生成】
问题的原因是在 scanf
函数中的格式化字符串中多了一个换行符 \n
。这个换行符的作用是让 scanf
函数在读取输入后跳过换行符。而你希望按回车键时结束输入,而不是输入一个换行符。
解决这个问题的方法是将 scanf
函数的格式化字符串中的 \n
删除,只保留 %f
。修改后的代码如下:
#include <stdio.h>
int main() {
float score;
printf("Please enter the score\n");
scanf("%f", &score);
int c = score / 10;
switch (c) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
printf("Grades belong to E");
break;
case 6:
printf("Grades belong to D");
break;
case 7:
printf("Grades belong to C");
break;
case 8:
printf("Grades belong to B");
break;
case 9:
printf("Grades belong to A");
break;
}
return 0;
}
这样修改之后,你就可以输入成绩后按回车键结束输入,并获得相应的等级输出了。
【相关推荐】