隨筆-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
              在默認(rèn)的情況下,數(shù)據(jù)是右對齊,通過“-”標(biāo)志來改變對齊方向
              width可以用于各種類型的數(shù)據(jù)轉(zhuǎn)換
              precision不是所有類型的數(shù)據(jù)都能使用precision,用于不同數(shù)據(jù)時的意義不同。
                   1.應(yīng)用于String時,它表示打印String時輸出字符的最大數(shù)量。
                   2.應(yīng)用于浮點數(shù)時,它表示小數(shù)部分顯示出來的位數(shù)(默認(rèn)是6位小數(shù)),如果小數(shù)位數(shù)過多四舍五入,太少尾部補零。
                   3.應(yīng)用于整數(shù)時,由于整數(shù)沒有小數(shù)部分,則會觸發(fā)異常。
              
               
          */
              
          }

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

          posted on 2011-05-24 12:47 secret_x15 閱讀(516) 評論(0)  編輯  收藏 所屬分類: java
          主站蜘蛛池模板: 泗洪县| 神池县| 毕节市| 佛教| 垣曲县| 房山区| 阳谷县| 巴青县| 贵定县| 栾城县| 邢台市| 博野县| 神池县| 泰州市| 庄河市| 霍林郭勒市| 苏尼特左旗| 桂林市| 县级市| 兰州市| 蓬安县| 葫芦岛市| 长武县| 常山县| 辛集市| 临邑县| 台东县| 苍南县| 榆林市| 台南县| 淮安市| 宜城市| 葵青区| 贡山| 朝阳市| 金华市| 汝阳县| 天门市| 西平县| 南康市| 竹溪县|