C 我的方程式一直是零,不知道为什么
这是我的全部代码。无论我做什么,我的方程都会一直为零。任何帮助都将不胜感激C 我的方程式一直是零,不知道为什么,c,equation-solving,C,Equation Solving,这是我的全部代码。无论我做什么,我的方程都会一直为零。任何帮助都将不胜感激 #include <stdio.h> #include <stdlib.h> #include <math.h> int main(void) { int x, y; float a,t; //Inputs printf("What is the speed that the jet is traveling in km/hr? \nWhat is the distance trav
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
int x, y;
float a,t;
//Inputs
printf("What is the speed that the jet is traveling in km/hr? \nWhat is the distance traveled in meters? \n");
scanf("%d , %d", &x, &y );
//Calculations
a = x * 1 / 60 * 1 / 60 * 1 / 60 * 1000 ;
t = sqrt( y * a / 2 ) ;
//Outputs
printf("The acceleration of the jet is %f meters per second squared. \n", a);
printf("The time it takes for the jet to reach takeoff speed is %f seconds. \n", t);
return 0;
}
#包括
#包括
#包括
内部主(空)
{
int x,y;
浮动a,t;
//投入
printf(“喷气机以公里/小时为单位行驶的速度是多少?\n以米为单位行驶的距离是多少?\n”);
scanf(“%d,%d”,&x,&y);
//计算
a=x*1/60*1/60*1/60*1/60*1000;
t=sqrt(y*a/2);
//输出
printf(“射流加速度为%f米/秒平方。\n”,a);
printf(“飞机达到起飞速度所需的时间为%f秒。\n”,t);
返回0;
}
您的第一个等式等于
a = ((((((x * 1) / 60) * 1) / 60) * 1) / 60) * 1000;
ie
或
即使你的a是一个浮点数,你方程的RHS也在做整数除法
因此,分配给x的任何小于216000的值都将导致0。
x
和y
也需要是float
。您可能需要将整数除法更改为浮点除法。在60后添加一个单点将解决这个问题(60.
)。@deamentiaemundi不一定,如果(出于某种原因)您想要强制执行整数速度。但不建议使用后者。1/60为0;乘以0,结果为零。使用小数点表示浮点常量:1.0/60.0
例如(我更喜欢尾随的0;不是每个人都关心)。我在60后添加了小数以表示浮点常量。我现在得到一个输出。谢谢
a = (x/(60*60*60)) * 1000;
a = (x/(216000)) * 1000;