亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 203 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號