我正在尝试计算BMI并为数字设置标签,但它会一直返回“NaN”而不是数字。
完整代码:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">
<fx:Script>
<![CDATA[
public var weightnum:Number;
public var heightnum:Number;
public var resultvar:Number;
protected function button1_clickHandler():void
{
weightnum = Number(weightvar);
heightnum = Number(heightvar);
resultvar = weightnum * 4.4 / (heightnum * heightnum);
resultstr.text = resultvar.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label left="30" top="30" text="Your Weight (lbs)"/>
<s:TextInput id="weightvar" restrict="0-9" x="30" y="53" width="80%"/>
<s:Label x="30" y="104" text="Your Height (feet)"/>
<s:TextInput id="heightvar" restrict="0-9" x="30" y="127" width="80%"/>
<s:Button x="30" y="202" label="Calculate" click="button1_clickHandler()"/>
<s:Label id="resultstr" x="30" y="253" text="" />
答案 0 :(得分:0)
使用number.toString()
而不是使用String()进行强制转换答案 1 :(得分:0)
您正在尝试将TextInput
转换为数字(Number(weightvar);
)。
这显然是行不通的。
您需要做的是将TextInput
的{{1}}属性转换为数字,就像这样:
text