Scala实现:将-999至999的整数列表转为英文单词列表
Hey Anna, let's build that translate function step by step using Scala's pattern matching—perfect for breaking down those tricky multi-digit numbers like 12 or 103!
Step 1: Break down the number-to-word logic
First, we need a helper function that converts a single integer (from -999 to 999) into its English word equivalent. We'll use pattern matching to handle different cases:
- Single digits and teens (0-19) have unique words, so we can match them directly.
- Tens values (20, 30, ..., 90) also have unique words.
- Two-digit numbers (like 21) are a combination of a ten and a unit, joined with a hyphen.
- Three-digit numbers split into a hundreds place plus the remainder (handling cases where the remainder is 0, like 200, vs non-zero, like 103).
- Negative numbers just get a "negative" prefix before the positive version of the number.
Step 2: Implement the helper function
Here's the helper function with all these cases covered:
def numberToWord(n: Int): String = { if (n < 0) s"negative ${numberToWord(-n)}" else n match { // 0-19 case 0 => "zero" case 1 => "one" case 2 => "two" case 3 => "three" case 4 => "four" case 5 => "five" case 6 => "six" case 7 => "seven" case 8 => "eight" case 9 => "nine" case 10 => "ten" case 11 => "eleven" case 12 => "twelve" case 13 => "thirteen" case 14 => "fourteen" case 15 => "fifteen" case 16 => "sixteen" case 17 => "seventeen" case 18 => "eighteen" case 19 => "nineteen" // Tens values case 20 => "twenty" case 30 => "thirty" case 40 => "forty" case 50 => "fifty" case 60 => "sixty" case 70 => "seventy" case 80 => "eighty" case 90 => "ninety" // Two-digit numbers (21-99, not exact tens) case num if num < 100 => val tens = num / 10 * 10 val units = num % 10 s"${numberToWord(tens)}-${numberToWord(units)}" // Three-digit numbers (100-999) case num if num < 1000 => val hundreds = num / 100 val remainder = num % 100 if (remainder == 0) s"${numberToWord(hundreds)} hundred" else s"${numberToWord(hundreds)} hundred ${numberToWord(remainder)}" // Fallback for numbers outside -999 to 999 (per your requirement, this might not be needed) case _ => "invalid number" } }
Step 3: Build the translate function
Now, the translate function is straightforward—we just map each number in the input list to its word using our helper function:
def translate(listofnumbers: List[Int]): List[String] = { listofnumbers.map(numberToWord) }
Test it out!
If you run your sample input:
println(translate(List(1, 2, 3, 4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19)))
You'll get the output:
List(one, two, three, four, five, six, seven, eight, nine, ten, eleven, twelve, thirteen, fourteen, fifteen, sixteen, seventeen, eighteen, nineteen)
And it works for multi-digit and negative numbers too! For example:
println(translate(List(12, 103, -45, 200))) // Output: List(twelve, one hundred three, negative forty-five, two hundred)
A quick note: We're not using mkString here—our function returns a List[String] directly, which matches your requirement perfectly.
内容的提问来源于stack exchange,提问作者Anna

