计算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(0x999999,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(0x666666,1);
button.graphics.drawRect(0,0,100,50);
button.graphics.endFill();
}
private function mouseRollOut(e:MouseEvent):void{
button.graphics.lineStyle(1);
button.graphics.beginFill(0x999999,1);
button.graphics.drawRect(0,0,100,50);
button.graphics.endFill();
}
}
}