第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在 C# 中將數(shù)字轉(zhuǎn)換為單詞

在 C# 中將數(shù)字轉(zhuǎn)換為單詞

C#
慕萊塢森 2023-12-17 10:39:54
我知道互聯(lián)網(wǎng)上有很多食譜,但我想自己掌握。使用簡(jiǎn)單的方法,我只想訪問(wèn)數(shù)組并獲取“WORD”即 = 用戶輸入的數(shù)字例如輸入33三十三所以我必須將 33 除以 10獲取第一個(gè)數(shù)字并乘以 10得到余數(shù)得到“三十”從十?dāng)?shù)組和“樹(shù)”從單位如何將信息與數(shù)組進(jìn)行比較并獲取重要信息? 對(duì)于循環(huán)?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 回答

?
喵喵時(shí)光機(jī)

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊

試試這個(gè)代碼:


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;

    }

}

}


您可以毫不費(fèi)力地將其擴(kuò)展到 1000 以上。 我相信在這里使用字典而不是列表要好得多。它速度快,而且您犯錯(cuò)誤的可能性較小。


查看完整回答
反對(duì) 回復(fù) 2023-12-17
  • 1 回答
  • 0 關(guān)注
  • 196 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)