使用公式速度=距离/时间计算时间

  • 本文关键字:时间 距离 计算 速度 c
  • 更新时间 :
  • 英文 :


使用公式计算时间 速度=距离/时间

但时间总是0我的输入是距离=10,速度=5,我的输出必须=2

#include<stdio.h>
int main()
{
    int a, b, c, d;
    char time, distance, speed;
    printf("Enter Your distance ",a);
    scanf("%d", &a);
    printf("Enter Your speed ",b);
    scanf("%d", &b);
    time=distance/speed;
    printf("time is %d ",time);
}

您使用的是整数(整数算术)而不是浮点数。

一个整数可以是四个字节,但不包含任何小数(0150035,但不能是3.1251)。浮点数也是四个字节(大多数时候),并且确实包含小数(3.14),但是,浮点数的整体范围较低且更难预测。

您还使用 char s(1 字节)。 1 字节 = 8 位,因此它们的最小值为 -128,最大值为 127。

试试这个:

float time, distance, speed;
time = distance / speed;

你的速度和距离是int所以你得到的时间是int .例如

distance=5 speed=2 time=5/2是 2.5,但为了使其int它被截断并变为 2。

另外,我无法弄清楚您在哪里将值分配给timespeeddistance ab您阅读。同样,将timedistancespeed char制作似乎不是一个好主意。

float time, distance, speed;
printf("Enter Your distance ");
scanf("%f", &distance);
printf("Enter Your speed ");
scanf("%f", &speed);
time=distance/speed;
printf("time is %f",time);

这应该可以正常工作。

拼写错误:
1. 您声明timedistance & speedchar
2.您以ab存储距离和速度的输入。
3. printf("Enter Your distance ",a);不是有效的输入语法。

试试这个

#include<stdio.h>
int main()
{
    double time, distance, speed;
    printf("Enter Your distance: ");
    scanf("%f", &distance);
    printf("Enter Your speed: ");
    scanf("%f", &speed);
    time=distance/speed;
    printf("time is %f ",time);
}

如果要使用十进制输入并且想要获得十进制输出,则应在浮点数中声明时间,距离和速度。

`#include<stdio.h>
int main()
{
    float time, distance, speed;
    printf("Enter Your distance: n");
    scanf("%.2fn", &distance);
    printf("Enter Your speed: n");
    scanf("%.2fn", &speed);
    time=distance/speed;
    printf("time is %.2f ",time);
}`

最新更新