Board logo

標題: printf() 函式 [打印本頁]

作者: tonyh    時間: 2013-4-20 16:08     標題: 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. }
複製代碼

作者: 劉漢文    時間: 2013-4-20 16:43

  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. }
複製代碼

作者: t2364705    時間: 2013-4-20 16:44

  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. }
複製代碼

作者: 黃博鴻    時間: 2013-4-20 16:44

  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. }
複製代碼

作者: t3742238    時間: 2013-4-20 16:45

  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. }
複製代碼

作者: 尤泓鈞    時間: 2013-4-20 16:47

  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. }
複製代碼

作者: 蔡昀佑    時間: 2013-4-20 16:47

  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. }
複製代碼





歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://seed.istak.org.tw/) Powered by Discuz! 7.2