返回列表 發帖

printf() 輸出練習

本帖最後由 tonyh 於 2019-7-3 21:50 編輯

試以 printf() 方法做字串輸出。
%d 整數
%s 字串
%S 轉換為大寫字母的字串
%.2f 四捨五入至小數點後2位


  1. public class Ch60
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         String str1="dog", str2="cat";
  6.         int a=3, b=5;
  7.         double total=21.3568;
  8.         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pounds.",a,str1,b,str2,total);
  9.     }
  10. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. import java.util.Scanner;
  2. public class Ch50 {
  3.         public static void main(String[] args){
  4.         int a = 3,b = 5;
  5.                 String str1 = "cat",str2="dog";
  6.                 double total = 45.213568;
  7.                 System.out.printf("There are %d %ss and %d %SS totally weigh %.2f pounds.",a,str1,b,str2,total);
  8.         }
  9. }
複製代碼
May

TOP

  1. public class Ch04
  2. {
  3.                 public static void main(String[] args)
  4.                 {
  5.                         String str1="dog",str2="cat";
  6.                         int a=3,b=5;
  7.                         double total=21.3568;
  8.                         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pounds.",a,str1,b,str2,total);
  9.                        
  10.                 }
  11.       
  12. }
複製代碼

TOP

  1. public class Ch24
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         String str1="dog", str2="cat";
  6.         int a=3, b=5;
  7.         double total=21.3568;
  8.         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pounds.",a,str1,b,str2,total);
  9.     }
  10. }
複製代碼

TOP

  1. public class Ch60
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         String str1="dog", str2="cat";
  6.         int a=3, b=5;
  7.         double total=21.3568;
  8.         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pounds.",a,str1,b,str2,total);
  9.     }
  10. }
複製代碼

TOP

  1. public class Ch35
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         String str1="dog", str2="cat";
  6.         int a=3, b=5;
  7.         double total=21.3568;
  8.         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pounds.",a,str1,b,str2,total);
  9.     }
  10. }
複製代碼

TOP

  1. import java.util.Scanner;

  2. public class Ch10
  3. {
  4.         public static void main(String args[])
  5.     {
  6.         String str1="dog",str2="cat";
  7.         int f=9,b=10;
  8.         double total=45.5543769;
  9.         System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f "
  10.                                    +"pounds.",f,str1,b,str2,total);  
  11.     }
  12. }
複製代碼

TOP

  1. public class Ch13
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 int a=3,;
  6.                 double c=21.32231;
  7.                 String d="你好",e="dogs";
  8.             System.out.printf("I hava %d %S and %.2f cats and they can say %s",
  9.                             a,e,c,d);
  10.         }
  11. }
複製代碼

TOP

返回列表