类似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]\n",x,y);
        System.out.printf("Row1:[%d %f]\n",x,y);
        /*output:
         *Row1:[5 5.332542]
         *Row1:[5 5.332542]
         *Row1:[5 5.332542]
         * 
         */
        
    }
    
}

 

posted @ 2016-03-20 17:32  zerocoin  阅读(117)  评论(0编辑  收藏  举报