You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言控制台输入衬衫价格后无法显示输出问题求助

问题排查与修复

你的程序卡在输入大码衬衫价格后无法显示结果,核心问题出在这行代码:

scanf("%f\n", &large);

问题原因

scanf的格式串里的\n完全多余。scanf遇到格式串中的空白字符(包括换行、空格、制表符)时,会持续跳过输入里的所有空白内容,直到读到非空白字符才会结束当前scanf调用。也就是说,你输入完大码价格按回车后,程序还在等你输入另一个非空白字符,根本没执行到后面的输出步骤。

修复方案

把那行代码里的\n删掉,改成:

scanf("%f", &large);

完整修正代码

int main(void)
{
   float small = 0.0; 
   float medium = 0.0; 
   float large = 0.0;
   float total = 0.0;
   printf("Set Shirt Prices\n");
   printf("================\n");
   printf("Enter the price for a SMALL shirt: $");
   scanf("%f", &small);
   printf("Enter the price for a MEDIUM shirt: $");
   scanf("%f", &medium);
   printf("Enter the price for a LARGE shirt: $");
   scanf("%f", &large); // 已移除多余的\n
   printf("Shirt Store Price List\n");
   printf("======================\n");
   printf("SMALL : $%f\n", small);
   printf("MEDIUM : $%f\n", medium);
   printf("LARGE : $%f\n", large);
   return 0;
}

新手小提示

  1. 输入价格时直接输入数字(比如19.99)即可,不要输入$,否则scanf会读取失败;
  2. 如果想让价格显示更规范(保留两位小数),可以把输出的%f改成%.2f,比如:
    printf("SMALL : $%.2f\n", small);
    

内容的提问来源于stack exchange,提问作者ysxapp

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 13:40:54