You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 % 10 will return a negative value if n is negative (e.g., -14 % 10 = -4). We'll use Math.Abs() to get the positive last digit regardless of input sign.
  • Mapping digits to words: We can use either a switch statement (great for simple, fixed mappings) or a Dictionary (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 of long.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 14:42:35