类似C语言格式化输出

类似C语言格式化输出

java se5引入的format方法可以用于PrintStream或PrintWriter对象,format方法模仿自C的printf(),

如果你比较怀旧的话,也可以用printf()。

package example;


public class Test  {
    public static void main(String[] args){
        int x=5;
        double y=5.332542;
        System.out.println("Row1:["+x+" "+y+"]");
        System.out.format("Row1:[%d %f]
",x,y);
        System.out.printf("Row1:[%d %f]
",x,y);
        /*output:
         *Row1:[5 5.332542]
         *Row1:[5 5.332542]
         *Row1:[5 5.332542]
         * 
         */
        
    }
    
}