返回列表 發帖

printf() 輸出練習

試以 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 Ch01

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


  10.         }

  11. }
複製代碼

TOP

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

TOP

  1. public class AS02 {

  2.         public static void main(String[] args) {
  3.                 String a="dog", b="cat";
  4.                 int x=7, y=4;
  5.                 double n=35.1493;
  6.                 System.out.printf("There are %d %Ss and %d %ss and totally weight %.2f pounds",x,a,y,b,n);

  7.         }

  8. }
複製代碼

TOP

  1. public class Ch10
  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 Ch01
  3. {      
  4.         public static void main(String[] args)
  5.     {
  6.                 String str1="dog", str2="cat";
  7.             int a=3, b=5;
  8.         double c=21.3568;
  9.         System.out.printf("There are %d %ss and %d %Ss totallt weight %.2f pounds",a,str1,b,str2,c);
  10.         }
  11. }   
複製代碼

TOP

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

TOP

  1. import java.util.Scanner;
  2. public class Ch05 {
  3.        
  4.         public static void main(String[] args) {
  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 weight %.2f pounds.",a,str1,b,str2,total);

  9.         }

  10. }
複製代碼

TOP

  1. public class Ch01 {

  2.         public static void main(String[]args)
  3.         {
  4.                 String str1="dog", str2="cat";
  5.                 int a=3, b=5;
  6.                 double total=21.3568;
  7.                 System.out.printf("There are %d %ss and %d %Ss totally weigh %.2f pound.",a,str1,b,str2,total);

  8.         }




  9. }
複製代碼

TOP

返回列表