我是一个非常有经验的Java程序员,具有C ++的背景知识,但是我只是在我的一个编程类中开始使用C,这使我发疯。这是第一个任务。应该计算球的体积或表面积。问题是,即使用户输入值,“半径”也等于零。 “模式”工作正常,这似乎很奇怪。这是代码:
#include <stdio.h>
#define PI 3.14
int main()
{
float radius;
int mode;
printf("\nPlease enter a non-negative radius of a sphere: ");
scanf("%f", &radius);
printf("Please enter 1 for volume, 2 for area: ");
scanf("%d", &mode);
if (radius = 0)
{
printf("\n\nPlease enter a positive radius, instead of %f.", radius);
}
else
{
float area = 4 * PI * radius * radius;
float volume = (4.0f / 2.0f) * PI * radius * radius * radius;
float result = 0;
if(mode == 1)
{
printf("\n\nYou are computing volume.");
result = volume;
}
else
{
printf("\n\nYou are computing area.");
result = area;
}
printf("\n\nThe result is %2f", result);
}
fflush(stdin);
getchar();
return 0;
}
知道为什么半径存储不正确吗?仅供参考-此代码大部分是预先编写的。我只是应该找到错误。
最佳答案
if (radius = 0)
应该是if (radius == 0)