C语言身高预测问题如何解决

img



int main()//a为父亲身高 
{
    int sex,sp,di;
    
    int a,b;
    double h;
    
        scanf("%d",&a);
        scanf("%d",&b);
        scanf("%c",&sex);
        scanf("%c",&sp);
        
    if(sex=='F')
    {
        h=(a*0.923+b)/2;
        }
    else if(sex=='M') {
        
        h=(a+b)/0.54;
                   }
                   
                   
                   
               double o,p;
               o=h;
               p=h;
               
               
               
        
    if(sp=='Y')
        {
        o=o*1.02;
        o=o-h;}
    else{
        o=0;
        }
        scanf("%c",&di);
    if(di=='Y')
        {
        p=p*1.015;
        p=p-h;}
    else
        {
        p=0;
        }
        
        
        h=h+o+p;
    printf("%.2f",h);
    
    return 0;
}

感觉if没有执行啊,运算结果总是为零

获取字符前,需要先清空缓冲区;

然后将各问答变量的数据类型改为字符类型即可。

修改如下:

参考链接:



#include <stdio.h>

int main() { //a为父亲身高


    char sex,sp,di; // 将各问答字段变量的数据类型改为字符类型char 
//    int sex,sp,di;

    int a,b;
    double h;

    scanf("%d",&a);
    scanf("%d",&b);
    fflush(stdin);  //清空缓冲区
    scanf("%c",&sex);
    fflush(stdin); //清空缓冲区
    scanf("%c",&sp);
//    printf("a=%d,b=%d,sex=%c,%d,sp=%c,%d\n",a,b,sex,sex,sp,sp); 
    

    if(sex=='F') {
    //    printf("F\n");
        h=(a*0.923+b)/2;
    } else if(sex=='M') {
    //    printf("M\n");
        h=(a+b)/0.54;
    }
//    printf("1,h=%f\n",h);


    double o,p;
    o=h;
    p=h;




    if(sp=='Y') {
        o=o*1.02;  
        o=o-h;
    } 
    else {
        o=0;
    }
//    printf("2,o=%f\n",o);

    fflush(stdin); //清空缓冲区
    scanf("%c",&di);
// printf("di=%c\n",di);
    if(di=='Y') { 
        p=p*1.015; 
        p=p-h;
    } 
    else {
        p=0;
    }


    h=h+o+p;
    printf("%.2f",h);

    return 0;
}

img

【以下回答由 GPT 生成】

问题的原因在于字符输入时,scanf函数会读取换行符"\n"作为输入值。在代码中,字符sex和字符sp的输入后,换行符会被接收到变量di中,导致if语句判断错误。解决这个问题,可以在读取字符前加入一个空的scanf语句来清除输入缓冲区中的换行符。修改后的代码如下所示:

#include <stdio.h>

int main()
{
    int sex, sp, di;
    int a, b;
    double h;

    scanf("%d", &a);
    scanf("%d", &b);
    scanf(" %c", &sex); // 加入空的scanf语句
    scanf(" %c", &sp); // 加入空的scanf语句

    if(sex == 'F')
    {
        h = (a * 0.923 + b) / 2;
    }
    else if(sex == 'M') 
    {
        h = (a + b) / 0.54;
    }

    double o, p;
    o = h;
    p = h;

    if(sp == 'Y')
    {
        o = o * 1.02;
        o = o - h;
    }
    else{
        o = 0;
    }

    scanf(" %c", &di); // 加入空的scanf语句

    if(di == 'Y')
    {
        p = p * 1.015;
        p = p - h;
    }
    else
    {
        p = 0;
    }

    h = h + o + p;
    printf("%.2f", h);

    return 0;
}

在修改后的代码中,使用" %c"而不是"%c"来读取字符,空格符" "会忽略掉前面的空白字符,包括换行符。这样就能够正确读取字符输入,解决了结果始终为零的问题。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^

供参考:

#include <stdio.h>
int main()
{
    double faHeight, moHeight, height;
    char   sex, sports, deit;
    printf("Please input faHeight  moHeight:");
    scanf("%lf %lf", &faHeight, &moHeight);
    printf("please you inputyour sex(F/M):");
    scanf(" %c", &sex);
    printf("Do you like sports(Y/N):");
    scanf(" %c", &sports);
    printf("Do you have good deit(Y/N):");
    scanf(" %c", &deit);
    if (sex == 'F')
        height = (faHeight * 0.923 + moHeight) / 2.0;
    if (sex == 'M')
        height = (faHeight + moHeight) * 0.54;
    if (sports == 'Y')
        height = height * (1 + 0.02);
    if (deit == 'Y')
        height = height * (1 + 0.015);
    printf("height=%lfcm\n", height);
    return 0;
}