使用 Java 中的 Formatter 格式化输出

java 8object oriented programmingprogramming

对于 Formatter,导入以下包 −

import java.util.Formatter;

现在创建一个 Formatter 对象 −

Formatter f1 = new Formatter();

现在,我们将使用 Formatter − 格式化输出

f1.format("Rank and Percentage of %s = %d %f", "John", 2, 98.5);

下面是一个例子 −

示例

import java.util.Formatter;
public class Demo {
    public static void main(String args[]) {
        Formatter f1 = new Formatter();
        Formatter f2 = new Formatter();
        f1.format("%s = %d %f 的排名和百分比", "John", 2, 98.5);
        System.out.println(f1.toString());
        f2.format("%s = %d %f 的排名和百分比", "David", "Warner", 1, 80.8);
        System.out.println(f2.toString());
    }
}

输出

John 的排名和百分比 = 2 98.500000
David Warner 的排名和百分比 = 1 80.800000

相关文章