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

Java中如何将数字i转为英文序数词(first/second等)输出

如何将Java中的数字转换为英文序数词输出?

要实现把循环中的数字i替换成对应的英文序数词,同时最后一个提示显示last,可以通过自定义序数词转换方法+循环内判断逻辑来实现,具体方案如下:

1. 编写序数词转换工具方法

先实现一个方法,把数字转换成对应的英文序数词,覆盖小数字的特殊写法和大数字的通用规则:

public static String getOrdinal(int num) {
    if (num <= 0) {
        return ""; // 处理非正整数的无效输入
    }
    // 处理1-5这类有特殊序数词的小数字
    switch (num) {
        case 1: return "first";
        case 2: return "second";
        case 3: return "third";
        case 4: return "fourth";
        case 5: return "fifth";
        // 可根据需求继续扩展更多常用数字的特殊写法
        default:
            // 处理大于5的数字,遵循通用序数词规则
            int lastDigit = num % 10;
            int lastTwoDigits = num % 100;
            String suffix;
            // 11-13是特殊情况,统一用th结尾
            if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
                suffix = "th";
            } else {
                switch (lastDigit) {
                    case 1: suffix = "st"; break;
                    case 2: suffix = "nd"; break;
                    case 3: suffix = "rd"; break;
                    default: suffix = "th"; break;
                }
            }
            return num + suffix;
    }
}

2. 结合循环逻辑生成目标提示

在原来的循环中,判断当前数字是否为最后一个,若是则直接用last,否则调用工具方法获取序数词,再拼接成目标字符串:

import java.util.Scanner;

public class DogNameInput {
    public static void main(String[] args) {
        int totalDogs = 3; // 示例:总共有3只狗
        Scanner input = new Scanner(System.in);

        for (int i = 1; i <= totalDogs; i++) {
            String ordinalText;
            // 判断是否是最后一个,是的话用last
            if (i == totalDogs) {
                ordinalText = "last";
            } else {
                ordinalText = getOrdinal(i);
            }
            // 拼接并输出提示语
            System.out.println("Please enter the name for the " + ordinalText + " dog.");
            String dogName = input.nextLine();
            // 这里可以添加对dogName的后续处理逻辑
        }
        input.close();
    }

    // 序数词转换方法
    public static String getOrdinal(int num) {
        if (num <= 0) {
            return "";
        }
        switch (num) {
            case 1: return "first";
            case 2: return "second";
            case 3: return "third";
            case 4: return "fourth";
            case 5: return "fifth";
            default:
                int lastDigit = num % 10;
                int lastTwoDigits = num % 100;
                String suffix;
                if (lastTwoDigits >= 11 && lastTwoDigits <= 13) {
                    suffix = "th";
                } else {
                    switch (lastDigit) {
                        case 1: suffix = "st"; break;
                        case 2: suffix = "nd"; break;
                        case 3: suffix = "rd"; break;
                        default: suffix = "th"; break;
                    }
                }
                return num + suffix;
        }
    }
}

效果验证

运行后控制台会输出:

Please enter the name for the first dog.
Please enter the name for the second dog.
Please enter the name for the last dog.

完全符合需求中的输出格式。

内容的提问来源于stack exchange,提问作者Angela A.A

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 05:46:22