对于三位数,我在执行此操作时遇到了一些麻烦。我可以对两位数执行此操作,但是当我将 if 语句添加到字符串 TwoDigit 时,它会告诉我检测到无法访问的代码。这是我尝试过的:-
{
class Program
{
static string[] digitWords =
{ "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
static string[] tenWords =
{ "", "", "twenty", "thirty", "forty",
"fifty", "sixty", "seventy", "eighty", "ninety" };
static string[] hundredWords = { "One-hundred", "two-hundred",
"three-hundred", "four-hundred", "five-hundred", "six-hundred",
"seven-hundred", "eight-hundred", "nine-hundred"}
static string TwoDigit(int num)
{
if (num < 0 || num > 99) return "";
if (num < 20) return digitWords[num];
if (num % 10 == 0)
return tenWords[num / 10];
else
return tenWords[num / 10] + "-" + digitWords[num % 10];
if (num % 100 == 0)
return digitWords[num / 100] + "-" + hundredWords[num % 100];
else
return digitWords[num / 100] + "-" + hundredWords[num % 100] + "-" + digitWords[num % 100];
}
static void Main(string[] args)
{
for (int i = 0; i <= 19; i++)
Console.WriteLine("{0}: {1}", i, TwoDigit(i));
for (int i = 20; i <= 99; i +=7)
Console.WriteLine("{0}: {1}", i, TwoDigit(i));
for (int i = 100; i <= 1100; i += 7)
Console.WriteLine("{0}: {1}", i, TwoDigit(i));
}
}
}