对于课程,我有一项任务,要求我将数值转换为单词等值词。太高了9999。
我使用dictionary命令实现了这一点,但在设置数组后无法执行此操作。 我们只是上课的第四周,所以对我来说很难。
这是我的数组
string[] ToWordsOne = new string[10] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
string[] ToWordsTen = new string[8] { "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string[] ToWordsTeens = new string[9] { "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
该程序似乎认识到这一点很好,但是当我输出代码时,它不会说出任何事情或崩溃(取决于我所做的更改。)
int i = 0;
string output = "";
while (i <= input.Length)
{
if (i == (input.Length))
{
output = output + " " + ToWordsOne[i];
}
if (i == (input.Length))
{
if (input == 1)
{
output = ToWordsTeens[i];
}
else
{
output = output + ToWordsTen[i];
}
}
if (i == (input.Length))
{
output = output + ToWordsOne[i] + " hundred" + " ";
}
if (i == (input.Length))
{
output = ToWordsOne[i] + " thousand" + " ";
}
i++;
这是我用来输出字典结果的代码。我知道我不能使用&#34; .length&#34;方法。但是我不知道从那里去哪里。
答案 0 :(得分:1)
根据这篇文章:converting-numbers-in-to-words-c-sharp您可以将数字转换为如下所示的单词。
public static string NumberToWords(int number)
{
if (number == 0)
return "zero";
if (number < 0)
return "minus " + NumberToWords(Math.Abs(number));
string words = "";
if ((number / 1000000) > 0)
{
words += NumberToWords(number / 1000000) + " million ";
number %= 1000000;
}
if ((number / 1000) > 0)
{
words += NumberToWords(number / 1000) + " thousand ";
number %= 1000;
}
if ((number / 100) > 0)
{
words += NumberToWords(number / 100) + " hundred ";
number %= 100;
}
if (number > 0)
{
if (words != "")
words += "and ";
var unitsMap = new[] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
var tensMap = new[] { "zero", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" };
if (number < 20)
words += unitsMap[number];
else
{
words += tensMap[number / 10];
if ((number % 10) > 0)
words += "-" + unitsMap[number % 10];
}
}
return words;
}