标题:简单的问题。帮我看下这个程序错哪了
只看楼主
yuanye2416
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2012-9-27
结帖率:92.31%
 问题点数:0 回复次数:3 
简单的问题。帮我看下这个程序错哪了
#include <stdio.h>
#include <math.h>
int main (void)
{
    int n;
    double sum_odd=0.0,sum_even=0;
    double ct_odd=0,ct_even=0;
    while (scanf("%d",&n)==1 && n!=0)
    {
        if (n % 2 == 1)
        {    sum_odd += n;
            ++ct_odd;
        }
        else
            
        {    sum_even += n;
                ++ct_even;
        }
    }   
        printf ("Number of evens: %d",sum_even);
        if (ct_even > 0) printf (" average : %Lf",sum_even/ct_even);
        printf ("\n");
        printf ("Nunber of odd : %d",sum_odd);
            if (ct_odd > 0) printf (" average : %Lf",sum_odd/ct_odd);
        printf("\n");
}

为什么"Number of evens 和odd都是0呢


[ 本帖最后由 yuanye2416 于 2012-10-25 15:12 编辑 ]
搜索更多相关主题的帖子: average include double 
2012-10-25 15:09
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
得分:0 
程序代码:
#include <stdio.h>
#include <math.h>
int main (void)
{
    int n;
    double sum_odd=0.0,sum_even=0;
    double ct_odd=0,ct_even=0;
    while (scanf("%d",&n)==1 && n!=0)
    {
        if (n % 2 == 1)
        {    sum_odd += n;
            ++ct_odd;
        }
        else
           
        {    sum_even += n;
                ++ct_even;
        }
    }   
        printf ("Number of evens: %d",sum_even);//你把%d换成%lf,因为你定义的是double型,相输出整数可以强制转换,printf ("Number of evens: %d",(int)sum_even);
        if (ct_even > 0) printf (" average : %Lf",sum_even/ct_even);
        printf ("\n");
        printf ("Nunber of odd : %d",sum_odd););//你把%d换成%lf
        if (ct_odd > 0) printf (" average : %Lf",sum_odd/ct_odd);
        printf("\n");
}

编程之路定要走完……
2012-10-25 15:32
C_戴忠意
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
威 望:2
帖 子:575
专家分:1349
注 册:2011-10-21
得分:0 
程序代码:
#include <stdio.h>
#include <math.h>
int main (void)
{
    int n;
    double sum_odd=0.0,sum_even=0;
    double ct_odd=0,ct_even=0;
    while (scanf("%d",&n)==1 && n!=0)
    {
        if (n % 2 == 1)
        {    sum_odd += n;
            ++ct_odd;
        }
        else
           
        {    sum_even += n;
                ++ct_even;
        }
    }   
        printf ("Number of evens: %d",sum_even);//你把%d换成%lf,因为你定义的是double型,相输出整数可以强制转换,printf ("Number of evens: %d",(int)sum_even);
        if (ct_even > 0) printf (" average : %Lf",sum_even/ct_even);
        printf ("\n");
        printf ("Nunber of odd : %d",sum_odd););//你把%d换成%lf
        if (ct_odd > 0) printf (" average : %Lf",sum_odd/ct_odd);
        printf("\n");
}

编程之路定要走完……
2012-10-25 15:32
张朝成
Rank: 4
等 级:业余侠客
帖 子:149
专家分:265
注 册:2012-3-22
得分:0 
输出错了,你改下就好了。
2012-10-25 19:54



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-384376-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.061322 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved