我正在尝试创建一个程序,根据给定的 Vo、a 和 tm 值计算 Vt 值。当我运行代码时,程序会询问第一个问题,然后在我给它一个值后,它会加快代码的运行速度,而不会让我为其他两个变量提供值。我正在用 C 编程。这是代码:
#include <stdio.h>
main () {
float Vt;
float Vo;
float a;
float tm;
printf(" At what time in flight do you wish to know the velocity? To the
nearest hundredth. :");
scanf(" %.2f", &tm);
printf (" What is the angle of trajectory? :");
scanf (" %.2f", &a);
printf (" What is the initial velocity? :");
scanf (" %.2f", &Vo);
float sina = sin (a);
float cosa = cos (a);
float tana = tan (a);
Vt = sqrt((pow((Vo * cosa), 2.0)) + (pow((Vo * sina - (9.8 *tm)), 2.0)));
printf("\n\n\n\n %.3f", Vt);
return 0;
}
最佳答案
您的 scanf 格式字符串无效。将它们更改为“%f”
。即改变
scanf("%.2f", &tm);
到scanf("%f", &tm);
scanf("%.2f", &a);
到scanf("%f", &a);
scanf("%.2f", &Vo);
到scanf("%f", &Vo);
如果您打算扫描最多 N 个字符,则必须使用"%Nf"
格式字符串(例如"%2f"
或 "%3f"
),其中 N 是大于 0 的整数。
关于c - 我该如何修复我的代码?我认为问题是第一个scanf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46376124/