C语言小白问题 ①代码如图1 运行结果如图2 为什么第三行显示0.0,哪里错了

C语言小白问题 ①代码如图1 运行结果如图2 为什么第三行显示0.0,哪里错了②或者像下文这样写,哪里错(语法错误不算。。)?为什么运行结果(如图3)不一样?

#include <stdio.h>
#define gasolineconversionfactor 3.785
#define distanceconversionfactor 1.609
int main()
{
float miles,gallons;

printf("Please enter the number of miles traveled.\n");
scanf("%f",&miles);
printf("Then please enter the number of gallons of gasoline.\n");
scanf("%f",&gallons);
printf("The number of miles per gallon is:%.1f\n",miles/gallons);
printf("In American way,it will be%.1f\n",gallons*gasolineconversionfactor/miles*distanceconversionfactor*100);
return 0;
}

这里多打了一个括号,你可以手动算一下,当你输入100 20后,计算结果为0.00470478558110627719080174021131,而你又用%.1f让输入只有一位小数,就只有0.0了,把%.1f改成%f就可以看到后面的小数

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-09-24

printf("In American way,it will be%.1f\n",gallons*gasolineconversionfactor/miles*distanceconversionfactor*100);

一个加了括号,一个没有加, 算法是不一样的。去试试你就知道了。

优先级的问题。

追问

在正常数学中加括号不是不影响吗,这里为什么影响?什么道理?

本回答被提问者和网友采纳
第2个回答  2016-09-23
因为你瞎打了一个括号
在printf中
第3个回答  2016-09-23
3.0 * 20 / 100 * 100 * 1.x
可不就是0.0吗

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网