为了账号安全,请及时绑定邮箱和手机立即绑定

在 C# 中将数字转换为单词

在 C# 中将数字转换为单词

C#
慕莱坞森 2023-12-17 10:39:54
我知道互联网上有很多食谱,但我想自己掌握。使用简单的方法,我只想访问数组并获取“WORD”即 = 用户输入的数字例如输入33三十三所以我必须将 33 除以 10获取第一个数字并乘以 10得到余数得到“三十”从十数组和“树”从单位如何将信息与数组进行比较并获取重要信息? 对于循环?int number;int i = 0;String[] units = new String[] { "one", "two", "three" };String[] tens = new String[] { "twenty", "thirty", "forty" };Console.WriteLine("Please enter a number");number = Convert.ToInt32(Console.ReadLine());if (number < 20){    Console.WriteLine("The number is");    Console.WriteLine(units[]);}else if (number > 20){    Console.WriteLine("The number is");    Console.WriteLine(tens[]);}
查看完整描述

1 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

试试这个代码:


using System;

using System.Collections.Generic;


namespace ConsoleApp2

{

class Program

{

    static void Main(string[] args)

    {

        Console.WriteLine("Hello World!");


        NumberToWordConverter nc = new NumberToWordConverter();



        Console.WriteLine(nc.ConvertNumberToWord(0));

        Console.WriteLine(nc.ConvertNumberToWord(5));

        Console.WriteLine(nc.ConvertNumberToWord(17));

        Console.WriteLine(nc.ConvertNumberToWord(37));

        Console.WriteLine(nc.ConvertNumberToWord(147));

        Console.WriteLine(nc.ConvertNumberToWord(252));

        Console.WriteLine(nc.ConvertNumberToWord(489));

        Console.WriteLine(nc.ConvertNumberToWord(900));

        Console.WriteLine(nc.ConvertNumberToWord(950));

        Console.WriteLine(nc.ConvertNumberToWord(999));



        Console.ReadLine();

    }

}



public class NumberToWordConverter

{

    private Dictionary<long, string> numWordDict = new Dictionary<long, string>();



    public NumberToWordConverter()

    {

        numWordDict.Add(0, "zero");

        numWordDict.Add(1, "one");

        numWordDict.Add(2, "two");

        numWordDict.Add(3, "three");

        numWordDict.Add(4, "four");

        numWordDict.Add(5, "five");

        numWordDict.Add(6, "six");

        numWordDict.Add(7, "seven");

        numWordDict.Add(8, "eight");

        numWordDict.Add(9, "nine");

        numWordDict.Add(10, "ten");

        numWordDict.Add(11, "eleven");

        numWordDict.Add(12, "twelve");

        numWordDict.Add(13, "thirteen");

        numWordDict.Add(14, "fourteen");

        numWordDict.Add(15, "fifteen");

        numWordDict.Add(16, "sixteen");

        numWordDict.Add(17, "seventeen");

        numWordDict.Add(18, "eightteen");

        numWordDict.Add(19, "nineteen");

        numWordDict.Add(20, "twenty");

        numWordDict.Add(30, "thirty");

        numWordDict.Add(40, "forty");

        numWordDict.Add(50, "fifty");

        numWordDict.Add(60, "sixty");

        numWordDict.Add(70, "seventy");

        numWordDict.Add(80, "eighty");

        numWordDict.Add(90, "ninety");

        numWordDict.Add(100, "hundred");


    }


    /// <summary>

    /// Only goes up to 900 but you can modify this code to make it go up higher

    /// </summary>

    /// <param name="number"></param>

    /// <returns></returns>

    public string ConvertNumberToWord(long number)

    {

        string nstring = string.Empty;


        if (number == 0)

        {

            return numWordDict[number];

        }


        if(number < 20)

        {

            return numWordDict[number];

        }


        long hundreds = number / 100;

        number -= hundreds * 100;

        long tens = number / 10;

        number -= tens * 10;

        long ones = number;



        if (hundreds > 0)

        {

            nstring = numWordDict[hundreds] + " " + numWordDict[100];

        }


        if (tens > 0)

        {

            if (!string.IsNullOrWhiteSpace(nstring))

            {

                nstring += " and ";

            }


            nstring += numWordDict[tens * 10];

        }


        if (ones > 0)

        {

            if (!string.IsNullOrWhiteSpace(nstring))

            {

                nstring += " ";

            }


            nstring += numWordDict[ones];


        }


        return nstring;

    }

}

}


您可以毫不费力地将其扩展到 1000 以上。 我相信在这里使用字典而不是列表要好得多。它速度快,而且您犯错误的可能性较小。


查看完整回答
反对 回复 2023-12-17
  • 1 回答
  • 0 关注
  • 60 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信