Java ChoiceFormat类实现复数格式化

版权声明:最终解释权归属Hern所有,恒! https://blog.csdn.net/qq_36761831/article/details/82726830
package com.Hern;
import java.text.ChoiceFormat;

public class FormatPuralsChoice extends FormatPlurals {
    static double[] limits = {0, 1, 2};
    static String[] formats = {"reviews", "review", "reviews"};
    static ChoiceFormat plurlizedFormat = new ChoiceFormat(limits, formats);

    // 使用ChoiceFormat将数值转换为英语的文本
    static ChoiceFormat quantizedFormat = new ChoiceFormat("0#no reviews|1#one review|1<many reviews");

    // 测试数据
    static int[] data = {-1, 0, 1, 2, 3};

    public static void main(String[] args) {
        System.out.println("Pluralized format");
        for (int i : data) {
            System.out.println("Found " + i + " " + plurlizedFormat.format(i));
        }
        System.out.println("Quantized Format");
        for (int i : data) {
            System.out.println("Found " + quantizedFormat.format(i));
        }
    }
}

class FormatPlurals {
    public static void main(String[] args) {
        report(0);
        report(1);
        report(2);
    }

    public static void report(int n) {
        System.out.println("We used" + n + " item" + (n == 1 ? "" : "s"));
    }
}
package com.Hern;
import java.util.*;
import java.math.*;
import java.text.ChoiceFormat;

public class Test {
    static double[] limits = {0, 1, 2};
    static String[] formats = {"reviews", "review", "reviews"};
    static ChoiceFormat plurlizedFormat = new ChoiceFormat(limits, formats);

    // 使用ChoiceFormat将数值转换为英语的文本
    static ChoiceFormat quantizedFormat = new ChoiceFormat("0#no reviews|1#one review|1<many reviews");

    // 测试数据
    static int[] data = {-1, 0, 1, 2, 3};

    public static void main(String[] args) {
        System.out.println("Pluralized format方法");
        for (int i : data) {
            System.out.println("Found " + i + " " + plurlizedFormat.format(i));
        }
        
        System.out.println();
        System.out.println("Quantized Format方法");
        for (int i : data) {
            System.out.println("Found " + quantizedFormat.format(i));
        }
    }
}   

/*运行结果
Pluralized format方法
Found -1 reviews
Found 0 reviews
Found 1 review
Found 2 reviews
Found 3 reviews

Quantized Format方法
Found no reviews
Found no reviews
Found one review
Found many reviews
Found many reviews
*/

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/82726830