作业帮 > 综合 > 作业

请大哥大姐叔叔阿姨们来看看啊···

来源:学生作业帮 编辑:百度作业网作业帮 分类:综合作业 时间:2024/07/18 09:45:28
请大哥大姐叔叔阿姨们来看看啊···
我要编一个程序计算圆柱体的表面积和体积.
我这样编的:
#include
int main(void)
{
float r,h;
double p=3.1415926536;
float A=2*p*r*r + 2*p*r*h;
float V =p*r*r*h;
printf("Please input the radius r and the height h of the cylinder.\n"); scanf("%f %f.\n",&r,&h);
printf("The cylinder has surface area A =%f and the volume V = %f",A,V);
return 0;
}
我生成解决方案后它提示说:从“double”转换到“float”,可能丢失数据.
还有:使用了未初始化的局部变量“r”
使用了未初始化的局部变量“h”
请问怎么解决呀?大哥大姐叔叔阿姨们一定帮帮我啊!
请大哥大姐叔叔阿姨们来看看啊···
你把程序的顺序弄反了吧.
应该先输入r,h的值 在执行A=2*p*r*r+2*p*r*h;
V=p*r*r*h;
至于double到float的问题,在我电脑上运行时没有问题.
实在不行你把pi的类型改成float嘛,有效数字是取6位吧.
我稍微改了一下你的程序,没有报错,而且运行结果正确.
#include
int main(void)
{
float r,h;
float p=3.141592;
float A,V;
printf("Please input the radius r and the height h of the cylinder.\n");
scanf("%f %f.\n",&r,&h);
A=2*p*r*r+2*p*r*h;
V =p*r*r*h;
printf("The cylinder has surface area A =%f and the volume V = %f",A,V);
return 0;
}