使用 Formatter 格式化 Java 輸出


對於 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("Rank and Percentage of %s = %d %f", "John", 2, 98.5);
      System.out.println(f1.toString());
      f2.format("Rank and Percentage of %s %s = %d %f", "David", "Warner", 1, 80.8);
      System.out.println(f2.toString());
   }
}

輸出

Rank and Percentage of John = 2 98.500000
Rank and Percentage of David Warner = 1 80.800000

更新於: 30-7 月 - 2019

268 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告