返回列表 發帖

printf() 函式

本帖最後由 tonyh 於 2013-4-20 16:35 編輯

利用 printf() 函式, 規範輸出字串的格式.
  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a="There are %d cats and %d dogs.%n"; // %d 輸出整數  %n 換行
  6.         String b="There are %f cats and %.2f dogs.%n"; //%f 輸出浮點數  .2 四捨五入至小數後第二位
  7.         String c="There are %s cats, %d dogs, %s pigs, and %d rabbits.%n";  //%s 字串
  8.         System.out.printf(a,5,7);
  9.         System.out.printf(b,5.0,7.0);
  10.         System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a="There are %d cats and %d dogs.%n";
  6.         String b="There are %f cats and %.2f dogs.%n";
  7.         String c="There are %s cats, %d dogs, %s pigs, and %d rabbits.%n";  
  8.         System.out.printf(a,5,7);
  9.         System.out.printf(b,5.0,7.0);
  10.         System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

TOP

  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a="There are %d cats and %d dogs.%n";
  6.         String b="There are %f cats and %.2f dogs.%n";
  7.         String c="There are %s cats, %d dogs, %s pigs, and %d rabbits.%n";  
  8.         System.out.printf(a,5,7);
  9.         System.out.printf(b,5.0,7.0);
  10.         System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

TOP

  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a="There are %d cats and %d dogs.%n";
  6.         String b="There are %f cats and %.2f dogs.%n";
  7.         String c="There are %s cats, %d dogs, %s pigs, and %d rabbits.%n";  
  8.         System.out.printf(a,5,7);
  9.         System.out.printf(b,5.0,7.0);
  10.         System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

TOP

  1. public class ch63
  2. {
  3.      public static void main(String args[])
  4.      {
  5.          String a="There are %d cats and %d dogs.%n";
  6.          String b="There are %f cats and %.2f dogs.%n";
  7.          String c="There are %s cats and %d dogs,%s pigs,and %d rabbits.%n";
  8.          System.out.printf(a,5,7);
  9.          System.out.printf(b,5.0,7.0);
  10.          System.out.printf(c,"5",7,"2",3);
  11.      }
  12. }
複製代碼

TOP

  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.          String a="There are %d cats and %d dogs.%n";
  6.          String b="There are %f cats and %.2f dogs.%n";
  7.          String c="There are %s cats, %d dogs, %s pig, and %d ribbit.%n";
  8.          System.out.printf(a,5,7);
  9.          System.out.printf(b,5.0,7.0);
  10.          System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

TOP

  1. public class ch63
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         String a="There are %d cats and %d dogs.%n";
  6.         String b="There are %f cats and %.2f dogs.%n";
  7.         String c="There are %s cats, %d dogs, %s pigs, and %d rabbits.%n";  
  8.         System.out.printf(a,5,7);
  9.         System.out.printf(b,5.0,7.0);
  10.         System.out.printf(c,"5",7,"2",3);
  11.     }
  12. }
複製代碼

TOP

返回列表