如何在C#中将数字的最后一位转换为对应英文单词
Solution to Convert Last Digit of a Number to English Word
Got it, let's polish your existing code to implement this functionality smoothly. Here's a step-by-step breakdown of what you need to add:
Key Considerations First
- Handle negative numbers: In C#,
n % 10will return a negative value ifnis negative (e.g.,-14 % 10 = -4). We'll useMath.Abs()to get the positive last digit regardless of input sign. - Mapping digits to words: We can use either a
switchstatement (great for simple, fixed mappings) or aDictionary(cleaner if you might expand the mapping later).
Option 1: Using Switch Statement (Straightforward)
This is perfect for your simple digit-to-word mapping:
using System; namespace Program { class LastDigit { static void Main() { Console.WriteLine("Please write your number:"); // Add error handling in case user enters non-numeric input if (long.TryParse(Console.ReadLine(), out long n)) { long lastDigit = Math.Abs(n % 10); // Ensure we get a positive digit string word = lastDigit switch { 0 => "zero", 1 => "One", 2 => "two", 3 => "Three", 4 => "Four", 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Nine", _ => "" // This case won't hit since lastDigit is 0-9 }; Console.WriteLine($"The last digit is {lastDigit}, which translates to: {word}"); } else { Console.WriteLine("Oops, that's not a valid number. Please try again."); } } } }
Option 2: Using Dictionary (Scalable)
If you ever need to adjust the word mappings or add more rules, a dictionary is easier to maintain:
using System; using System.Collections.Generic; namespace Program { class LastDigit { static void Main() { Console.WriteLine("Please write your number:"); if (long.TryParse(Console.ReadLine(), out long n)) { long lastDigit = Math.Abs(n % 10); // Define the digit-to-word mapping Dictionary<long, string> digitWords = new Dictionary<long, string> { {0, "zero"}, {1, "One"}, {2, "two"}, {3, "Three"}, {4, "Four"}, {5, "Five"}, {6, "Six"}, {7, "Seven"}, {8, "Eight"}, {9, "Nine"} }; // Retrieve the word (we know the key exists, so no need for TryGetValue here) string word = digitWords[lastDigit]; Console.WriteLine($"The last digit is {lastDigit}, which translates to: {word}"); } else { Console.WriteLine("Invalid input! Please enter a valid number."); } } } }
What We Added
- Input validation: Used
long.TryParse()instead oflong.Parse()to avoid crashes if the user enters non-numeric text (like letters or symbols). - Negative number handling:
Math.Abs(n % 10)ensures we always get a positive digit from 0-9. - Digit-to-word mapping: Implemented two clean approaches to map each digit to its corresponding English word, matching your examples (e.g., 2→"two", 4→"Four", 0→"zero").
- Output: Added a clear message to display the result to the user.
Test Cases to Verify
- Input
14→ Output: The last digit is 4, which translates to: Four - Input
12→ Output: The last digit is 2, which translates to: two - Input
10→ Output: The last digit is 0, which translates to: zero - Input
-27→ Output: The last digit is 7, which translates to: Seven - Input
abc→ Output: Oops, that's not a valid number. Please try again.
内容的提问来源于stack exchange,提问作者Monny Ahmeti
相关产品推荐
相关产品推荐

