【问题标题】:C programming printf, scanf [closed]C 编程 printf、scanf [关闭]
【发布时间】:2013-04-15 18:32:11
【问题描述】:

您好,我已经编写了这段代码,但输出如下:

您旅行了多少英里?:(您显示的次数)
你消耗了多少加仑汽油?:

然后它只是跳过扫描并移动到下一个位置,

#include <stdio.h>
#include <string.h>
#include <float.h>

int main ()
{

    float mile;
    float gallon;

    printf("How many Miles have u traveled?: ");
    scanf("%.1f",&mile);
    printf("How many gallons of gasoline have u consumed?: ");
    scanf("%.1f",&gallon);

} 

【问题讨论】:

  • 顺便说一句,不需要 float.h 和 string.h

标签: c floating-point include printf scanf


【解决方案1】:

您不应该格式化输入。所以只需使用%f 作为scanf 的第一个参数

int main ()
{

    float mile;
    float gallon;

    printf("How many Miles have u traveled?: ");
    scanf("%f",&mile);
    printf("How many gallons of gasoline have u consumed?: ");
    scanf("%f",&gallon);

} 

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多