计算BMI指数测量是否肥胖

今天把上次制作的测量是否肥胖的程序修改了一下,现在这个程序可不是玩具了,已经能计算BMI值,并根据BMI值测量是否肥胖。关于算法,在Google上百度了一下,居然发现都是男女统一的标准。

8.12更新,修改男女计算分类,按照BMI算法不分男女。改动不大,修改过的代码就不放出了。

主要改动在于界面元素,使用了两个静态常量,修改了计算方法。继续把修改过的代码放出。

BodyStandardTest类:

package {
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    [SWF(width="300",height="375")]

    public class BodyStandardTest extends Sprite
    {
        private var stageOutline:Shape;
        private var introText:TextField;
        private var tPleaseInputHeight:TextField;
        private var tInputHeight:TextField;
        private var tPleaseInputWeight:TextField;
        private var tInputWeight:TextField;
        private var tResult:TextField;
        private static const TEXTFIELD_WIDTH=50;
        private static const TEXTFIELD_HEIGHT=18;
        public function BodyStandardTest()
        {
            stage.scaleMode=StageScaleMode.NO_SCALE;

            stageOutline=new Shape();
            stageOutline.graphics.lineStyle(1);
            stageOutline.graphics.drawRect(0,0,300,375);
            addChild(stageOutline);

            introText=new TextField;
            introText.text=”输入你的身高和体重,以测试你的身材是否肥胖”;
            introText.autoSize=TextFieldAutoSize.LEFT;
            introText.selectable=false;
            introText.x=stage.width/2-introText.width/2;
            introText.y=30;
            addChild(introText);
            tPleaseInputHeight=new TextField();
            tPleaseInputHeight.text=”身高(cm):”;
            tPleaseInputHeight.autoSize=TextFieldAutoSize.LEFT;
            tPleaseInputHeight.selectable=false;
            tInputHeight=new TextField();
            tInputHeight.x=60;
            tInputHeight.width=BodyStandardTest.TEXTFIELD_WIDTH;
            tInputHeight.height=BodyStandardTest.TEXTFIELD_HEIGHT;
            tInputHeight.type=TextFieldType.INPUT;
            tInputHeight.border=true;
            tInputHeight.background=true;
            var heightContainer:Sprite=new Sprite();
            addChild(heightContainer);
            heightContainer.addChild(tPleaseInputHeight);
            heightContainer.addChild(tInputHeight);
            heightContainer.y=80;
            heightContainer.x=stage.width/2-10-heightContainer.width;
            tPleaseInputWeight=new TextField();
            tPleaseInputWeight.text=”体重(kg):”;
            tPleaseInputWeight.autoSize=TextFieldAutoSize.LEFT;
            tPleaseInputWeight.selectable=false;

            tInputWeight=new TextField();
            tInputWeight.x=60;
            tInputWeight.width=BodyStandardTest.TEXTFIELD_WIDTH;
            tInputWeight.height=BodyStandardTest.TEXTFIELD_HEIGHT;
            tInputWeight.type=TextFieldType.INPUT;
            tInputWeight.border=true;
            tInputWeight.background=true;
            var widthContainer:Sprite=new Sprite();
            addChild(widthContainer);
            widthContainer.addChild(tPleaseInputWeight);
            widthContainer.addChild(tInputWeight);
            widthContainer.y=80;
            widthContainer.x=stage.width/2+10;
            var bmiIntro:TextField=new TextField();
            with(bmiIntro){
                x=50;
                y=150;
                text=”BMI指数(身体质量指数,简称体质指数 英文为Body Mass Index,简称BMI),是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。主要用于统计用途。当我们需要比较及分析一个人的体重对于不同高度的人所带来的健康影响时,BMI值是一个中立而可靠的指标。”;
                selectable=false;
                wordWrap=true;
                width=200;
                autoSize=TextFieldAutoSize.LEFT;
            }
            addChild(bmiIntro);
            var calculateMale:calculateButton=new calculateButton(”计算男人”);
            calculateMale.x=stage.width/2-calculateMale.width-10;
            calculateMale.y=300;
            var calculateFemale:calculateButton=new calculateButton(”计算女人”);
            calculateFemale.x=stage.width/2+10;
            calculateFemale.y=300;
            addChild(calculateMale);
            addChild(calculateFemale);
            tResult=new TextField();
            tResult.x=50;
            tResult.y=110;
            tResult.selectable=false;
            tResult.text=”";
            tResult.autoSize=TextFieldAutoSize.LEFT;
            addChild(tResult);
            calculateMale.addEventListener(MouseEvent.CLICK,calculateM);
            calculateFemale.addEventListener(MouseEvent.CLICK,calculateF);
        }
        private function calculateBMI(height:Number,weight:Number):Number{
            height/=100;
            return weight/height/height;
        }
        private function checkValue(textfield:TextField):Boolean{
            var str:String=textfield.text;
            for(var i:int=0;i<str.length;i++){
                if(str.charCodeAt(i)>57||str.charCodeAt(i)<48){
                    return false;
                }
                trace(str.charCodeAt(i));
            }
            return true;
        }
        private function process(bmi:Number):String{
            if(bmi<18){
                return “你的身材偏瘦”;
            }else if(bmi>=18&&bmi<25){
                return “你的身材正常”;
            }else if(bmi>=25&&bmi<30){
                return “你已经超重”;
            }else if(bmi>=30&&bmi<35){
                return “轻度肥胖”;
            }else if(bmi>=35&&bmi<40){
                return “中度肥胖”;
            }else{
                return “重度肥胖”;
            }
        }
        private function calculateM(e:MouseEvent):void{
            if(checkValue(tInputHeight)&&checkValue(tInputWeight)){
                var height:Number=Number(tInputHeight.text);
                var weight:Number=Number(tInputWeight.text);
                var bmiNumber:Number=Math.round(calculateBMI(height,weight)/.1)*.1;
                tResult.text=String(bmiNumber)+”\n”+process(bmiNumber);
            }
        }
        private function calculateF(e:MouseEvent):void{
            if(checkValue(tInputHeight)&&checkValue(tInputWeight)){
                var height:Number=Number(tInputHeight.text);
                var weight:Number=Number(tInputWeight.text);
                var bmiNumber=Math.round(calculateBMI(height,weight)/.1)*.1;
                tResult.text=String(bmiNumber)+”\n”+process(bmiNumber);
            }
        }
    }
}

CalculateButton类:

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.text.*;
    class calculateButton extends Sprite
    {
        private var button:Sprite
        public function calculateButton(inputText:String)
        {
            button=new Sprite();
            button.graphics.lineStyle(1);
            button.graphics.beginFill(0×999999,1);
            button.graphics.drawRect(0,0,100,50);
            button.graphics.endFill();
            addChild(button);
            var textfield:TextField=new TextField();
            textfield.text=inputText;
            textfield.selectable=false;
            textfield.textColor=0xffffff;
            var tFormat:TextFormat=new TextFormat();
            tFormat.size=20;
            textfield.setTextFormat(tFormat);
            textfield.autoSize=TextFieldAutoSize.LEFT;
            textfield.x=this.width/2-textfield.width/2;
            textfield.y=this.height/2-textfield.height/2;
            addChild(textfield);
            this.addEventListener(MouseEvent.MOUSE_OVER,mouseRollOver);
            this.addEventListener(MouseEvent.MOUSE_OUT,mouseRollOut);
        }
        private function mouseRollOver(e:MouseEvent):void{
            button.graphics.lineStyle(1);
            button.graphics.beginFill(0×666666,1);
            button.graphics.drawRect(0,0,100,50);
            button.graphics.endFill();
        }
        private function mouseRollOut(e:MouseEvent):void{
            button.graphics.lineStyle(1);
            button.graphics.beginFill(0×999999,1);
            button.graphics.drawRect(0,0,100,50);
            button.graphics.endFill();
        }
    }
}

7 comments:

  1. smigoo, 11. August 2008, 16:12

    代码很长,很明显…我也没看懂。
    哈哈,这次做的真的可以测BMI值了,我还是标准的,嘿嘿,经过我的测试,原来以我的身高吃到63kg也算是正常,45的话就偏瘦了。这个国际标准真是好宽松啊,女性找到自信心的福音!不过我想偏瘦…..

    [Reply]

    SoleilNeon Reply:

    老婆我修改一下代码让你偏瘦吧 哈哈

    [Reply]

     
  2. Guilty辰, 11. August 2008, 19:58

    哎呀,这回对了内
    不过我还是不标准
    5555555555555555555555555555555

    [Reply]

     
  3. Chada, 13. August 2008, 13:22

    它说,你的身材很标准^^

    不过我也测试了一下,我这个身高,标准体重的范围差值将近20公斤哇,是有点太宽松了。

    [Reply]

     
  4. 寸碧点点, 15. August 2008, 13:16

    哇咔咔~~~
    :D
    刚开始没看清楚单位是km,结果一输数字进去产生了“意想不到”的效果,吓得我差点要跳起来,还好后来认识到错误后得到了惊喜的return,开心~~~

    [Reply]

    SoleilNeon Reply:

    呵呵,单位要看清楚,不然结果会很惊人的。

    [Reply]

     
  5. MY-Hou, 17. August 2008, 2:19

    結果讓我吃驚…居然是正常…

    [Reply]

     

Write a comment: