隨筆-9  評論-2  文章-0  trackbacks-0
          import java.util.Formatter;
          public class Receipt {
              
          private double total = 0;
              
          private Formatter f = new Formatter(System.out);
              
              
          public void printTitle(){
                   f.format(
          "%-15s   %5s   %10s\n""Item","Qty","Price");
                   f.format(
          "%-15s   %5s   %10s\n""----","---","-----");
              }
              
              
          public void print(String name,int qty,double price){
                   f.format(
          "%-15.15s   %5d   %10.2f\n", name,qty,price);
                   total 
          += price;
              }
              
              
          public void printTotal(){
               f.format(
          "%-15.15s   %5s   %10.2f\n""Tax","",total*0.06);
               f.format(
          "%-15.15s   %5s   %10s\n""","","-----");
               f.format(
          "%-15s   %5s   %10.2f\n""Total","",total*1.06);
              }
              
              
          public static void main(String[] args) {
                   Receipt receipt 
          = new Receipt();
                   receipt.printTitle();
                   receipt.print(
          "Jack's Magic Beans"44.25);
                   receipt.print(
          "Princess Peas"35.1);
                   receipt.print(
          "Three Bears Porridge"114.29);
                   receipt.printTotal();
              }
              
              
          /*
              %[argument_index$][flag][width][.precision]conversion
              在默認的情況下,數據是右對齊,通過“-”標志來改變對齊方向
              width可以用于各種類型的數據轉換
              precision不是所有類型的數據都能使用precision,用于不同數據時的意義不同。
                   1.應用于String時,它表示打印String時輸出字符的最大數量。
                   2.應用于浮點數時,它表示小數部分顯示出來的位數(默認是6位小數),如果小數位數過多四舍五入,太少尾部補零。
                   3.應用于整數時,由于整數沒有小數部分,則會觸發異常。
              
               
          */
              
          }

            類型轉換字符
          d 整數類型
          e 浮點數(科學計數)
          c Unicode字符
          x 整數(十六進制)
          b Boolean值
          h 散列碼(十六進制)
          s String % 字符%
          f 浮點數(十進制)        

          posted on 2011-05-24 12:47 secret_x15 閱讀(518) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 北流市| 大方县| 泌阳县| 乌恰县| 承德市| 遂宁市| 泽普县| 芦溪县| 马山县| 安塞县| 桓仁| 元江| 桃园市| 民丰县| 高阳县| 灯塔市| 汾阳市| 彭水| 大竹县| 上林县| 盘锦市| 丹寨县| 三门峡市| 深泽县| 边坝县| 平潭县| 乌恰县| 大庆市| 洪雅县| 崇信县| 开原市| 利辛县| 河南省| 锦屏县| 石屏县| 荣昌县| 涪陵区| 梅州市| 项城市| 望城县| 通山县|