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

我知道互联网上有很多食谱,但我想自己掌握。

使用简单的方法,我只想访问数组并获取“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[]);

}


慕莱坞森
浏览 122回答 1
1回答

喵喵时光机

试试这个代码:using System;using System.Collections.Generic;namespace ConsoleApp2{class Program{&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Hello World!");&nbsp; &nbsp; &nbsp; &nbsp; NumberToWordConverter nc = new NumberToWordConverter();&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(0));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(5));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(17));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(37));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(147));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(252));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(489));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(900));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(950));&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(nc.ConvertNumberToWord(999));&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }}public class NumberToWordConverter{&nbsp; &nbsp; private Dictionary<long, string> numWordDict = new Dictionary<long, string>();&nbsp; &nbsp; public NumberToWordConverter()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(0, "zero");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(1, "one");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(2, "two");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(3, "three");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(4, "four");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(5, "five");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(6, "six");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(7, "seven");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(8, "eight");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(9, "nine");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(10, "ten");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(11, "eleven");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(12, "twelve");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(13, "thirteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(14, "fourteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(15, "fifteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(16, "sixteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(17, "seventeen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(18, "eightteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(19, "nineteen");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(20, "twenty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(30, "thirty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(40, "forty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(50, "fifty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(60, "sixty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(70, "seventy");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(80, "eighty");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(90, "ninety");&nbsp; &nbsp; &nbsp; &nbsp; numWordDict.Add(100, "hundred");&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Only goes up to 900 but you can modify this code to make it go up higher&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="number"></param>&nbsp; &nbsp; /// <returns></returns>&nbsp; &nbsp; public string ConvertNumberToWord(long number)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string nstring = string.Empty;&nbsp; &nbsp; &nbsp; &nbsp; if (number == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return numWordDict[number];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(number < 20)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return numWordDict[number];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; long hundreds = number / 100;&nbsp; &nbsp; &nbsp; &nbsp; number -= hundreds * 100;&nbsp; &nbsp; &nbsp; &nbsp; long tens = number / 10;&nbsp; &nbsp; &nbsp; &nbsp; number -= tens * 10;&nbsp; &nbsp; &nbsp; &nbsp; long ones = number;&nbsp; &nbsp; &nbsp; &nbsp; if (hundreds > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nstring = numWordDict[hundreds] + " " + numWordDict[100];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (tens > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrWhiteSpace(nstring))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nstring += " and ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nstring += numWordDict[tens * 10];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (ones > 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!string.IsNullOrWhiteSpace(nstring))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nstring += " ";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nstring += numWordDict[ones];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return nstring;&nbsp; &nbsp; }}}您可以毫不费力地将其扩展到 1000 以上。 我相信在这里使用字典而不是列表要好得多。它速度快,而且您犯错误的可能性较小。
打开App,查看更多内容
随时随地看视频慕课网APP