| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1457 人关注过本帖
标题:新人挠头求助!头都要秃了!多重if-else语句总是无法得出正确结果
只看楼主 加入收藏
Huminnity
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-2-8
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:4 
新人挠头求助!头都要秃了!多重if-else语句总是无法得出正确结果
//以下是我打的代码,一般都是输入weight为80,height为2,得到结果为20,可是最后的判断结果总是Underweight: less than 18.5。
//用if{...}if{...}if{...}if{...}也是同样的判断结果。
//曾经尝试删去包含“Underweight: less than 18.5”的那个判断条件,最后直接得到结果20,没有其他判断结果。
//将整型int换成浮点型float也是出现同样的问题
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int weight, height, BMI;//定义体重、身高
    int a = BMI = weight/(height*height);//定义身体质量指数BMI
    printf("Please enter weight(KG): ");
    scanf("%d", &weight);//输入体重
    printf("Please enter height(M): ");
    scanf("%d", &height);//输入身高
    printf("a = BMI = weight / (height * height) = %d\n", weight/(height*height));//得到结果
    if(a >= 18.5)
    {
        printf("Normal: between 18.5 and 24.9");
    }
    else if(a >= 25)
    {
        printf("Overweight: between 25 and 29.9");
    }
    else if(a >= 30)
    {
        printf("Obese: 30 or greater");
    }
    else
    {
        printf("Underweight: less than 18.5");
    }
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: 结果 判断 if int printf 
2019-02-08 23:26
李少iii
Rank: 4
来 自:KUNMING
等 级:业余侠客
威 望:2
帖 子:67
专家分:245
注 册:2018-10-4
收藏
得分:10 
int a = BMI = weight/(height*height);//此处用到了除法操作,所以不能声明为整型,应该
声明为double类型,即可

我不曾拥有梦想,但我坚信我有未来……
2019-02-09 00:20
lolooze
Rank: 2
等 级:论坛游民
帖 子:9
专家分:21
注 册:2018-11-28
收藏
得分:10 
你的a没有被赋值。
程序代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int weight, height, BMI;//定义体重、身高
    int a;
    printf("Please enter weight(KG): ");
    scanf("%d", &weight);//输入体重
    printf("Please enter height(M): ");
    scanf("%d", &height);//输入身高
    a = BMI = weight/(height*height);
    printf("a = BMI = weight / (height * height) = %d\n",a);//得到结果
    if(a >= 18.5)
    {
        printf("Normal: between 18.5 and 24.9");
    }
    else if(a >= 25)
    {
        printf("Overweight: between 25 and 29.9");
    }
    else if(a >= 30)
    {
        printf("Obese: 30 or greater");
    }
    else
    {
        printf("Underweight: less than 18.5");
    }
    system("pause");
    return 0;
}

这样改就行了,输入身高体重后面对a赋值。

[此贴子已经被作者于2019-2-9 01:29编辑过]

2019-02-09 01:27
Huminnity
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-2-8
收藏
得分:0 
回复 2楼 李少iii
原来是这样,没试过double,是我疏忽了,谢谢了
2019-02-09 09:41
Huminnity
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2019-2-8
收藏
得分:0 
回复 3楼 lolooze
是这样了,代码打得太乱,忘了检查赋值,谢谢了
2019-02-09 09:42
快速回复:新人挠头求助!头都要秃了!多重if-else语句总是无法得出正确结果
数据加载中...
 
   



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

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