This question already has answers here:
Closed 4 years ago.
float variables in C [duplicate]
(5个答案)
有人能解释一下为什么“扫描”功能在下面的代码中不起作用吗?我该怎么做才能解决这个问题?谢谢!
int main() {
    int age, sumage;
    float mean;
    sumage = 0;
    for (int a = 1; a <= 20; a = a + 1) {
        printf("Enter age: \n");
        scanf("%d", &age);
        sumage = sumage + age;
    }
    mean = sumage/20;
    printf("mean = é %f \n" , mean);
    return 0;
}

最佳答案

mean = sumage/20;

这里sumage是一个整数。因此除法是整数除法。
更改为(float)sumage / 20.0以获得预期结果。

关于c - 扫描功能在循环内不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26964880/

10-13 09:31