首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不将3位数字转换为单词

不将3位数字转换为单词
EN

Stack Overflow用户
提问于 2018-07-25 05:51:07
回答 3查看 57关注 0票数 0

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

代码语言:javascript
复制
// 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 = "";
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-25 06:43:50

在100到999之间的数字的if语句中,您可以这样写

代码语言:javascript
复制
tensDigit = Math.floor(num / 10);

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

代码语言:javascript
复制
tensDigit = Math.floor(num / 10) % 10;
票数 1
EN

Stack Overflow用户

发布于 2018-07-25 07:11:52

很抱歉指出这一点,但你的代码很长,而且不是算法。这是一个有效的方法:

代码语言:javascript
复制
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;
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2018-07-25 07:18:51

在此条件下(num >= 100 && num <= 999)中该行的问题

代码语言:javascript
复制
 tensDigit = Math.floor(num  / 10);

这一行的结果是11、12或13,...etc。因此,tens()函数的条件将不会得到满足。

将此行修改为:

代码语言:javascript
复制
tensDigit = Math.floor(num % 100 / 10);

告诉我它有没有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51508025

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档