创建一个c#程序,将用户输入的数字作为输入并打印在单词上

时间:2015-10-17 05:13:38

标签: c# numbers console-application words digits

无论输入是什么,它都应该用文字打印答案(没有限制)例如输入是2它应该用单词“TWO”回答几个例子来理解12 =“十二”,51 =“五十一”,1000 =“一千”,2005年=“二千零五”等等......

2 个答案:

答案 0 :(得分:0)

查看精彩的Humanizer项目。它完全符合您的需求。

3501.ToWords() => "three thousand five hundred and one"

答案 1 :(得分:0)

Try this approach:
It's not a complete code, you can improve it by recursive call with
proper range checking etc.

Create a function with input parameter range from 0 to 99 (as shown below):
private string DigitToTextConverter(int digit)
    {
        string digitText = "";
        switch (digit)
        {
            case 0:
                digitText = "zero";
                break;
            case 1:
                digitText = "one ";
                break;
            case 2:
                digitText = "two ";
                break;
            case 3:
                digitText = "three ";
                break;
            case 4:
                digitText = "four ";
                break;
            case 5:
                digitText = "five ";
                break;
            case 6:
                digitText = "six ";
                break;
            case 7:
                digitText = "seven ";
                break;
            case 8:
                digitText = "eight ";
                break;
            case 9:
               .....;
            case 10:
               .....
            case 99:
               .....
            default:
                break;
        }

        return digitText;
    } 

//Call this function with appropriate parameter: (suppose user entered 1234)

 var userInput = 1234;
 var digitText = new StringBuilder();

 var quotient = userInput / 1000;
 string convertedText = DigitToTextConverter(quotient);
 digitText.Append(convertedText + " thousand");
 digitText.AppendFormat(" ");
 var remainder = userInput % 1000;

 quotient = remainder / 100;
 convertedText = DigitToTextConverter(quotient);
 digitText.Append(convertedText + " thundred");
 digitText.AppendFormat(" ");
 remainder = remainder % 100;

 //Complete remaining portion, better to have a function with recursive call
 string finalText = digitText.ToString();