1

我正在尝试计算 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="" />

4

2 回答 2

0

而不是使用 String() 进行强制转换,而是使用 number.toString()

于 2012-10-05T03:53:33.993 回答
0

您正在尝试将TextInput's 转换为数字 ( Number(weightvar);)。那显然行不通。

您需要做的是将TextInput'text属性转换为数字,就像这样:

weightnum = Number(weightvar.text);
于 2012-10-05T05:35:41.203 回答