请问,我写的一个计算平均值的C语言小程序,为什么在最后询问是否“再来一次”的时候没询问就退出了

#include <stdio.h>#include <stdlib.h>int main(){ printf("欢迎使用平均数计算工具!\n\n"); short int i,o; char y; double shuru,shuchu; do { shuchu = 0; printf("请输入数据数量:"); scanf("%d", &o); for(i = 1; i <= o; i++) { printf("请输入第%d个数据:", i); scanf("%lf", &shuru); shuchu += shuru; } shuchu /= o; printf("\n平均值为:%.2lf", shuchu); printf(" 要再来一次吗? Y/N\n"); scanf("%c", &y); }while(y == 'Y'); printf("\n感谢您的使用!!"); return 0;}//图片是运行结果

scanf %c不太稳定,你一次输入至少要按回车的,按的回车被当成了第二个字符。

解决办法在scanf之前加上:

fseek(stdin, 0, SEEK_END);

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答