不将 3 位数字转换为单词

Does not convert 3 digit numbers to words

以下代码用于将1-999之间的任意数字转换为单词。但是,该代码不会将其转换为百倍数以外的任何 3 位数字。例如,它将 567 打印为五百七。该程序还能够打印 1 到 100 之间的所有数字。请指教。谢谢!

// This line makes the button, btnConvert wait for a mouse click
// When the button is clicked, the convertNumber function is called
btnConvert.addEventListener(MouseEvent.CLICK, convertNumber);

// These lines make the textinputs wait for a mouse click
// When any of these components are clicked, the clearLabels function is called
txtinNumber.addEventListener(MouseEvent.CLICK, clearLabels);

// Declare Global Variables
var num:int;         // number from 10 - 99
var teensDigit:int;   // the tens digit
var onesDigit:int;   // the ones digit
var hundredsDigit:int;
var tensDigit:int;

// This is the convertNumber function
// e:MouseEvent is the click event experienced by the button
// void indicates that the function does not return a value
function convertNumber(e:MouseEvent):void
{ 
    getData();
    if (num < 1 || num > 999){
        lblOutput.text = "Invalid number. Enter a number between 1 and 999 inclusive.";
    }
    else if (num >= 1 && num <= 9){      
        onesDigit = num ;
        ones();
    }

    else if (num >= 10 && num<= 19){     
        tensDigit = Math.floor(num / 10);
        onesDigit = num % 10;
        teens();
    }

    else if (num >=20 && num<= 99){         
        tensDigit = Math.floor(num / 10);
        onesDigit = num % 10;
        tens();
        ones();
    }

    else if (num >= 100 && num <= 999){   
        hundredsDigit = Math.floor(num / 100);
        tensDigit = Math.floor(num / 10);
        onesDigit = num % 10;

        hundreds();
        tens();
        ones();
    }



}

// This is the getData function
// It gets the number from the user
function getData()
{
    // complete the code here
    num = int(txtinNumber.text);
}

// This is the tens function
// It outputs the word representation of 20, 30, 40,..,90
function tens()
{
    if (tensDigit == 2)
        lblOutput.text += "Twenty";

    if (tensDigit == 3)
        lblOutput.text += "Thirty";

    if (tensDigit == 4)
        lblOutput.text += "Fourty";

    if (tensDigit == 5)
        lblOutput.text += "Fifty";

    if (tensDigit == 6)
        lblOutput.text += "Sixty";

    if (tensDigit == 7)
        lblOutput.text += "Seventy";

    if (tensDigit == 8)
        lblOutput.text += "Eighty";

    if (tensDigit == 9)
        lblOutput.text += "Ninety";
}

// This is the ones function
// It outputs the word representaion for any number from 1 - 9 inclusive
function ones()
{
    if (onesDigit == 1)
        lblOutput.text += " " + "One";

    if (onesDigit == 2)
        lblOutput.text += " " + "Two" + " ";

    if (onesDigit == 3)
        lblOutput.text += " " + "Three";

    if (onesDigit == 4)
        lblOutput.text += " " + "Four";

    if (onesDigit == 5)
        lblOutput.text += " " + "Five";

    if (onesDigit == 6)
        lblOutput.text += " " + "Six";

    if (onesDigit == 7)
        lblOutput.text += " " + "Seven";

    if (onesDigit == 8)
        lblOutput.text += " " + "Eight";

    if (onesDigit == 9)
        lblOutput.text += " " + "Nine";
}

// This is the teens function
// It outputs the word representation for any number from 10 - 19 inclusive
function teens()
{
    if (onesDigit == 0)
        lblOutput.text += "Ten";

    if (onesDigit == 1)
        lblOutput.text += "Eleven";

    if (onesDigit == 2)
        lblOutput.text += "Twelve";

    if (onesDigit == 3)
        lblOutput.text += "Thirteen";

    if (onesDigit == 4)
        lblOutput.text += "Fourteen";

    if (onesDigit == 5)
        lblOutput.text += "Fifteen";

    if (onesDigit == 6)
        lblOutput.text += "Sixteen";

    if (onesDigit == 7)
        lblOutput.text += "Seventeen";

    if (onesDigit == 8)
        lblOutput.text += "Eighteen";

    if (onesDigit == 9)
        lblOutput.text += "Nineteen";
}


function hundreds()
{
    if (hundredsDigit == 1)
        lblOutput.text += " " + "One Hundred"

    if (hundredsDigit == 2)
        lblOutput.text += " " + "Two Hundred";

    if (hundredsDigit == 3)
        lblOutput.text += " " +  "Three Hundred";

    if (hundredsDigit == 4)
        lblOutput.text += " " +  "Four Hundred";

    if (hundredsDigit == 5)
        lblOutput.text += " " +  "Five Hundred";

    if (hundredsDigit == 6)
        lblOutput.text += " " +  "Six Hundred";

    if (hundredsDigit == 7)
        lblOutput.text += " " +  "Seven Hundred";

    if (hundredsDigit == 8)
        lblOutput.text += " " +  "Eight Hundred";

    if (hundredsDigit == 9)
        lblOutput.text += " " +  "Nine Hundred";
}


// This is the clearLabels function
// e:MouseEvent is the click event experienced by the textInput
// void indicates that the function does not return a value
function clearLabels(e:MouseEvent):void
{
     lblOutput.text = "";
}

if 100 到 999 之间的数字语句中,您写

tensDigit = Math.floor(num / 10);

但是,对于数字 567,Math.floor(num / 10); 计算为 56,这是一个不包含在您的 tens() 函数中的数字。要解决此问题,tensDigit 应设置为

tensDigit = Math.floor(num / 10) % 10;

很抱歉指出这一点,但您的代码很长且非算法。这是工作的:

package assortie
{
    import flash.display.Sprite;

    public class SpellNumber extends Sprite
    {
        // Class constructor and tests.
        public function SpellNumber() 
        {
            super();

            trace(toWords(567));
            trace(toWords(7));
            trace(toWords(56));
            trace(toWords(19));
            trace(toWords(913));
        }

        static private const ONES :Array = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];
        static private const TENS :Array = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
        static private const TEENS:Array = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];

        // This method converts numbers fom 1 to 999 to words.
        static private function toWords(value:int):String
        {
            var result:String = "";

            // Unexpected data cases.
            if (value < 1)   return "Number is out of bounds: " + value + " is zero or less.";
            if (value > 999) return "Number is out of bounds: " + value + " is thousand or greater.";

            // Figure out hundreds.
            var aHun:int = value / 100;

            if (aHun > 0)
            {
                // Remove hundreds from the given value.
                value %= 100;

                // Form the words for hundreds.
                result += ONES[aHun] + " Hundred";
            }

            // Check if rest is teens.
            if ((value > 9) && (value < 20))
            {
                // Add one space if result is not empty.
                if (result) result += " ";

                // Form the words for teens.
                result += TEENS[value - 10];

                // Return the result. There's nothing to do here any longer.
                return result;
            }

            // Figure out tens.
            var aTens:int = value / 10;

            if (aTens > 0)
            {
                // Remove tens from the rest of the value.
                value %= 10;

                // Add one space if result is not empty.
                if (result) result += " ";

                // Form the words for tens.
                result += TENS[aTens];
            }

            // Figure out the last digit.
            if (value > 0)
            {
                // Add one space if result is not empty.
                if (result) result += " ";

                // Form the words for the last digit.
                result += ONES[value];
            }

            return result;
        }
    }
}

在这种情况下这一行的问题 (num >= 100 && num <= 999)

 tensDigit = Math.floor(num  / 10);

这一行的结果是 11 或 12 或 13,...等等。所以,tens() 函数的条件将不满足。

将此行修改为:

tensDigit = Math.floor(num % 100 / 10);

告诉我它是否有效。