【C语言不简单】任务-身高预测—简单if语句的运用

【C语言不简单】任务-身高预测—简单if语句的运用,题目如下:

每个父母都关心自己孩子成年后的身高,据有关生理卫生知识与数理统计分析可知,小孩成年后的身高与其父母的身高、自身的性别、饮食习惯与体育锻炼情况等密切相关。

设faheight为其父身高,moheight为其母身高,身高预测公式为

男性成年后的身高=(faheight+moheight) *0.54(cm)

女性成年后的身高=(faheight*0.923+moheight)/2(cm)

此外,如果喜爱体育锻炼,那么可增加身高2.3%;如果有良好的卫生饮食习惯,那么可增加身高1.5%。


请大家按照你自己的设计进行编码。

这个案例中涉及多个变量的输入和赋值:教材中给出的代码如下:

#include 
void main( )
{
  char sex;					/*孩子性别*/
  char sports;				/*是否喜欢体育运动*/
  char diet;				/*是否有良好的饮食习惯*/
  float myheight;			/*孩子身高*/
  float faheight;			/*父亲身高*/
  float moheight;			/*母亲身高*/
  printf("你是男孩(b)  还是女孩(g)?");
  scanf("%1s",&sex);
  printf("输入你爸爸的身高(cm):");
  scanf("%f",&faheight);
  printf("输入你妈妈的身高(cm):");
  scanf("%f",&moheight);
  printf("你是否喜欢体育锻炼(Y/N)?");
  scanf("%1s",&sports);
  printf("是否有良好的饮食习惯等条件(Y/N)?");
  scanf("%1s",&diet);
  if (sex=='b'|| sex=='B')
	  myheight=(faheight+moheight) *0.54;
  if (sex=='g'|| sex=='G')
     myheight=(faheight*0.923+moheight)/2.0;
  if(sports=='Y'|| sports=='y')
     myheight=myheight* (1+0.023);
  if(diet=='Y'||diet=='y')
     myheight=myheight* (1+0.015);
  /*printf("Your future height will be%6.2f(cm)\n",  myheight);*/
printf("Your future height will be%6.2f(cm)\n",  myheight);

}

但是怎么也调试不通,在变量的赋值出错了。

修改一下,方法1,修改每个变量的赋值 :

#include 

int main() {
    char sex;                   /* 孩子性别 */
    char sports;                /* 是否喜欢体育运动 */
    char diet;                  /* 是否有良好的饮食习惯 */
    float myheight;             /* 孩子身高 */
    float faheight;             /* 父亲身高 */
    float moheight;             /* 母亲身高 */

    printf("你是男孩(b)  还是女孩(g)?");
    if (scanf(" %c", &sex) != 1) {
        printf("输入错误,请输入有效的字符。\n");
        return 1;
    }

    printf("输入你爸爸的身高(cm):");
    if (scanf("%f", &faheight) != 1) {
        printf("输入错误,请输入有效的身高数值。\n");
        return 1;
    }

    printf("输入你妈妈的身高(cm):");
    if (scanf("%f", &moheight) != 1) {
        printf("输入错误,请输入有效的身高数值。\n");
        return 1;
    }

    printf("你是否喜欢体育锻炼(Y/N)?");
    if (scanf(" %c", &sports) != 1) {
        printf("输入错误,请输入有效的字符。\n");
        return 1;
    }

    printf("是否有良好的饮食习惯等条件(Y/N)?");
    if (scanf(" %c", &diet) != 1) {
        printf("输入错误,请输入有效的字符。\n");
        return 1;
    }

    if (sex == 'b' || sex == 'B') {
        myheight = (faheight + moheight) * 0.54;
    } else if (sex == 'g' || sex == 'G') {
        myheight = (faheight * 0.923 + moheight) / 2.0;
    } else {
        printf("输入的性别无效,请输入 'b' 或 'g'。\n");
        return 1;
    }

    if (sports == 'Y' || sports == 'y') {
        myheight = myheight * (1 + 0.023);
    }

    if (diet == 'Y' || diet == 'y') {
        myheight = myheight * (1 + 0.015);
    }

    printf("Your future height will be %6.2f(cm)\n", myheight);

    return 0;
}    

修改一下,方法2,在一行中赋值所有的变量 :

#include 

int main() {
    char sex;                   /* 孩子性别 */
    char sports;                /* 是否喜欢体育运动 */
    char diet;                  /* 是否有良好的饮食习惯 */
    float myheight;             /* 孩子身高 */
    float faheight;             /* 父亲身高 */
    float moheight;             /* 母亲身高 */

    printf("input sex sports diet  faheight moheight ");
    scanf("%c%c%c%f%f",&sex,&sports,&diet,&faheight,&moheight);


    if (sex == 'b' || sex == 'B') {
        myheight = (faheight + moheight) * 0.54;
    } else if (sex == 'g' || sex == 'G') {
        myheight = (faheight * 0.923 + moheight) / 2.0;
    } else {
        printf("输入的性别无效,请输入 'b' 或 'g'。\n");
        return 1;
    }

    if (sports == 'Y' || sports == 'y') {
        myheight = myheight * (1 + 0.023);
    }

    if (diet == 'Y' || diet == 'y') {
        myheight = myheight * (1 + 0.015);
    }

    printf("Your future height will be %6.2f(cm)\n", myheight);

    return 0;
}    

【C语言不简单】任务-身高预测—简单if语句的运用_第1张图片

你可能感兴趣的:(C语言程序设计,c语言,开发语言)