0

ボタンをクリックすると、パーセントを小数に変換しようとしています。ユーザー入力用の入力テキスト フィールドと、その下に別の入力テキスト フィールドがあります。これは、ボタンをクリックした後に結果を表示する動的テキスト フィールドです。

メインと数学用のユーティリティ クラスの 2 つのクラスがあります。私の問題は、メイン クラスの util クラスから関数を呼び出してこれを機能させる方法がわからないことです。

この最後の部分が機能しません。ここに私のコードがあります:

public class Main extends Sprite
{
    private var pResult:TextField;
    private var pResult2:TextField;

    public function Main()
    {
        super();

        // calling all the functions below

        // adding my graphics to the display
        var baseDBase = new PDBase();
        this.addChild(base);
        base.x = -70;
        base.y = 30;

        //changing the font format
        var format:TextFormat = new TextFormat();//adding the object
        format.size = 14;//font sizing
        format.align = TextFormatAlign.LEFT;//font align

        //result text field
        pResult = new TextField();//adding the object
        this.addChild(pResult);//displaying the object
        pResult.border = true;//setting the border
        pResult.borderColor = 0x30FF00;//setting border color
        pResult.textColor = 0x000000;//setting the font color
        pResult.x = 28;//position left or right
        pResult.y = 70;//position up or down
        pResult.width = 142;// changing width
        pResult.height = 20;// changing height
        pResult.type = TextFieldType.INPUT;//making sure the text area is for input
        pResult.defaultTextFormat = format;//sets text format to defult
        pResult.maxChars = 32;//text limit of characters
        pResult.restrict = "0-9.";//fonts used only

        pResult2 = new TextField();//adding the object
        this.addChild(pResult2);//displaying the object
        pResult2.border = true;//setting the border
        pResult2.borderColor = 0x30FF00;//setting border color
        pResult2.textColor = 0x000000;//setting the font color
        pResult2.x = 28;//position left or right
        pResult2.y = 96;//position up or down
        pResult2.width = 142;// changing width
        pResult2.height = 20;// changing height
        pResult2.type = TextFieldType.DYNAMIC;//making sure the text area is for input
        pResult2.defaultTextFormat = format;//sets text format to defult
        pResult2.maxChars = 32;//text limit of characters
        pResult2.restrict = "0-9.";//fonts used only

        var button:Button = new Button("Calculate");
        this.addChild(button);
        button.x = 10;
        button.y = 130;
        button.addEventListener(MouseEvent.CLICK, btn_EventListener);
    }

    private function btn_EventListener(e:MouseEvent):void
    {
        MathUtil.percentToDecimal(percent)
        this.pResult2.text = percent;
    }
}

public class MathUtil
{

    public function MathUtil()
    {
    }

    public static function percentToDecimal(percentValue:Number):Number
    {
        var percent:Number = percentValue * 100;
        var roundedDecimal:Number = Math.round(percent);
        var percentResult:String = roundedDecimal;
        return percentResult;
    }
}

私もこのエラーが発生しています:

1067: Number 型の値を関連のない String 型に暗黙的に強制します。var percentResult:String = roundedDecimal;

4

1 に答える 1

0
this.pResult2.text = String(MathUtil.percentToDecimal(percent));

関数は値を返すことに注意してください。これを行うと、返された値が textfield に割り当てられます。「String()」は、返された数値を文字列にキャストしています。

編集:ああ、ここでキャストを使用できます:

var percentResult:String = String(roundedDecimal);
于 2012-05-18T23:53:53.483 に答える