男女理想体重计算的C程序

男女理想体重计算的C程序,c,C,我编写了以下程序,根据身高和性别检查男性和女性的理想体重: #include<stdio.h> #include<conio.h> int main() { int age; float height, mminima = 48, fminima = 45, iw; char gender; printf("Please Enter your Age,Height(in CM) and Gender\n"); scanf("%

我编写了以下程序,根据身高性别检查男性和女性的理想体重:

#include<stdio.h>
#include<conio.h>

int main()
{
    int age;
    float height, mminima = 48, fminima = 45, iw;
    char gender;

    printf("Please Enter your Age,Height(in CM) and Gender\n");

    scanf("%d\n%f\n%c", &age, &height, &gender);

    if((gender = 'M') && (height < 152.4))
    {
         iw = mminima - (152.4 - height) * 1.1;
         printf("\nYour idle Weight should be= %f", iw);
    }
    else
    {
         iw = mminima + (height - 152.4) * 1.1;
         printf("\nYour Idle Wight should be= %f", iw);
    }

    if((gender = 'F') && (height < 152.4))
    {
        iw = fminima - (152.4 - height) * 1.1;
        printf("\nYour Idle weight should be= %f", iw);
    }
    else
    {
        iw = fminima + (height - 152.4) * 1.1;
        printf("\nYour Idle weight should be= %f", iw);
    }

    getch();
    return 0;
}
#包括
#包括
int main()
{
智力年龄;
浮子高度,mminima=48,fminima=45,iw;
性别;
printf(“请输入您的年龄、身高(厘米)和性别\n”);
scanf(“%d\n%f\n%c”&年龄、身高和性别);
如果((性别='M')&(身高<152.4))
{
iw=mminima-(152.4-高度)*1.1;
printf(“\n您的空转重量应为=%f”,iw);
}
其他的
{
iw=mminima+(高度-152.4)*1.1;
printf(“\n您的空闲宽度应为=%f”,iw);
}
如果((性别='F')&(身高<152.4))
{
iw=fminima-(152.4-高度)*1.1;
printf(“\n您的空转重量应为=%f”,iw);
}
其他的
{
iw=fminima+(高度-152.4)*1.1;
printf(“\n您的空转重量应为=%f”,iw);
}
getch();
返回0;
}

但若并没有性别比较的话,输出结果总是显示出我对男性和女性的理想体重。为什么?我错在哪里?请帮忙

代码中的逻辑错误。首先你有一个打字错误

if((gender='M')&&(height<152.4))
然后你想做一个
else if
检查
gender
是否等于
'F'
,如下所示:

if(gender=='M')
{
    if(height<152.4)
    {
        iw=mminima-(152.4-height)*1.1;
        printf("\nYour idle Weight should be= %f",iw);
    }
    else 
    {
        iw=mminima+(height-152.4)*1.1;
        printf("\nYour Idle Wight should be= %f",iw);
    }
}
else if(gender=='F')
{
     if(height<152.4)
....
else if(性别='F')
{

if(身高您的if语句
if((性别=M')&(身高应纠正以下几个问题:

1) 错误的比较:

if(gender='M') // always true; should be if(gender=='M')
if((gender = 'F') // always true; should be if(gender=='M')
2) 该程序的逻辑有故障,它将打印女性和男性的
理想体重
如果高度高于
152.4

3) 大量的重复代码应该封装在函数中

4) 缺少错误性别符号的错误处理

5) 需要正确的拼写和打印。正确:
Idle Wigh

6) 无需将重量打印为
74.3456789
后面一位数字就足够了

7) 常量幻数可能应该在main之外定义,如:

#define MAGIC_HEIGHT 152.4
8) 使用
开关
外壳
可以提高程序的清晰度:

例如:

#include<stdio.h>
#include<conio.h>

#define M_MIN         48.0
#define F_MIN         45.0
#define FACTOR         1.1
#define MAGIC_HEIGHT 152.4

void print_ideal_weight(float iw)
{
    printf("\nYour Ideal weight should be = %.1f", iw);
}

float calculate_ideal_weight(float m, float height)
{
    float iw;

    if(height < MAGIC_HEIGHT){

        iw = m - (MAGIC_HEIGHT - height) * FACTOR;
    }
    else{
        iw = m + (height - MAGIC_HEIGHT) * FACTOR;
    }            
    return iw;
}

int main()
{
    int age;
    float height, iw;
    char gender;

    printf("Please Enter your Age, Height(in CM) and Gender(F/M): \n");

    scanf("%d\n%f\n%c", &age, &height, &gender);

    switch (gender)
    {
        case 'M':
            iw = calculate_ideal_weight(M_MIN,height);
            print_ideal_weight(iw);
        break;

        case 'F':
            iw = calculate_ideal_weight(F_MIN,height);
            print_ideal_weight(iw);
        break;

        default:
            printf("Unknown gender entered!\n");
        break;
    }

    getch();
    return 0;
}

创建一个公共函数,然后传递诸如性别和其他参数之类的参数并返回结果。这将是一种最好的编程方法。阅读。理解那篇文章的内容并使用它。user3121023非常感谢:)这肯定是一个错误,但输出仍然不符合预期,仍然会发生同样的事情。正如其他评论员已经说过的那样提到。
gender='F'
总是正确的。为了避免这种错误(每个人都会遇到这种情况-相信我),我建议总是把常量放在第一位。因此,不要写
gender=='F'
而是写
'F'==gender
。如果你错过了
=
'F'=gender
)编译器会马上告诉你。我想你指的是理想重量,不是闲置重量。只要注意他(和你)做了一个作业,而不是逻辑等式。你的宝贵答案增加了我的知识。
#include<stdio.h>
#include<conio.h>

#define M_MIN         48.0
#define F_MIN         45.0
#define FACTOR         1.1
#define MAGIC_HEIGHT 152.4

void print_ideal_weight(float iw)
{
    printf("\nYour Ideal weight should be = %.1f", iw);
}

float calculate_ideal_weight(float m, float height)
{
    float iw;

    if(height < MAGIC_HEIGHT){

        iw = m - (MAGIC_HEIGHT - height) * FACTOR;
    }
    else{
        iw = m + (height - MAGIC_HEIGHT) * FACTOR;
    }            
    return iw;
}

int main()
{
    int age;
    float height, iw;
    char gender;

    printf("Please Enter your Age, Height(in CM) and Gender(F/M): \n");

    scanf("%d\n%f\n%c", &age, &height, &gender);

    switch (gender)
    {
        case 'M':
            iw = calculate_ideal_weight(M_MIN,height);
            print_ideal_weight(iw);
        break;

        case 'F':
            iw = calculate_ideal_weight(F_MIN,height);
            print_ideal_weight(iw);
        break;

        default:
            printf("Unknown gender entered!\n");
        break;
    }

    getch();
    return 0;
}
Please Enter your Age, Height(in CM) and Gender(F/M):                                                             
25                                                                                                                
176                                                                                                               
M                                                                                                                 

Your Ideal weight should be = 74.0