我正在尝试创建一个程序来计算给定值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;
}
答案 0 :(得分:3)
您的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的整数。