为什么是否锻炼那里输不了?怎样改?

img

#include
#include
int main()
{
    char sex,sports,diet,F,M,Y,N;
    float faHeight,moHeight,Height;

    printf("请输入你的性别(F女M男):");
    scanf("%c",&sex);

    printf("请输入父亲的身高(cm):");
    scanf("%f",&faHeight);

    printf("请输入母亲的身高(cm):");
    scanf("%f",&moHeight);

    printf("是否喜爱体育锻炼(Y/N):");
    scanf("%c",&sports);

    printf("是否有良好的饮食习惯(Y/N):");
    scanf("%c",&diet);

    if(sex=='F')
        Height=(faHeight*0.923+moHeight)*0.5;
    if(sex=='M')
        Height=(faHeight+moHeight)*0.54;

    if(sports=='Y')
        Height*=(1+0.02);
    if(diet=='Y')
        Height*=(1+0.015);

        printf("你的预测身高为:%f",Height);
    system("pause");
    return 0;
}

scanf("%c",&sports);前面要加getchar(),接收掉前面输入数据后的换行符,否则sports会自动接收换行符的。同样的scanf("%c",&diet);前面也要加getchar()

被小修改了!详情看代码。注意逻辑关系以及scanf函数的特性掌握。

img

#include<stdio.h>

int main()
{
    char sex,sports,diet,F,M,Y,N;
    float faHeight,moHeight,Height;
    printf("请输入你的性别(F女M男):");
    scanf("%c",&sex);
    
    
    printf("请输入父亲的身高(cm):");
    scanf("%f",&faHeight);
   
     
    printf("请输入母亲的身高(cm):");
    scanf("%f",&moHeight);
  
    
    printf("是否喜爱体育锻炼(Y/N):");
    scanf(" %c",&sports);

    printf("是否有良好的饮食习惯(Y/N):");
    scanf(" %c",&diet);
   
    if(sex=='F')
        Height=(faHeight*0.923+moHeight)*0.5;
    else if(sex=='M')//根据逻辑关系应该是用else if的,虽然用if也不影响结果。
        Height=(faHeight+moHeight)*0.54;
    if(sports=='Y')
        Height*=(1+0.02);
    if(diet=='Y')
        Height*=(1+0.015);
    printf("你的预测身高为:%.2f",Height);
    
    //system("pause");
    return 0;
}