返回列表 發帖

20120707 上課內容

上課大綱:TQC 考前練習
) i. s2 G% ~$ k$ R+ T4 J練習題目:TQC107、TQC108、TQC204、TQC207

本帖最後由 jerryclass 於 2012-7-7 11:42 編輯 4 z* z  `7 J2 |1 b

1 m, L1 }; d3 i  F9 ?TQC 107
  1. public class TQC107 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub

  7.                 if (args.length < 2) {
  8.                         System.out.println("最少需要輸入2個數字");
  9.                 } else {
  10.                         double sum = 0;

  11.                         double count = args.length - 1;

  12.                         for (int i = 1; i < args.length; i++) {
  13.                                 sum = sum + Integer.valueOf(args[i]);
  14.                         }

  15.                         if (args[0].equals("0")) {
  16.                                 System.out.printf("%.0f", sum / count);
  17.                         } else if (args[0].equals("1")) {
  18.                                 System.out.printf("%.1f", sum / count);
  19.                         } else if (args[0].equals("2")) {
  20.                                 System.out.printf("%.2f", sum / count);
  21.                         }
  22.                 }
  23.         }

  24. }
複製代碼

TOP

回復 2# jerryclass 3 c2 J5 G& X4 u0 [7 m$ I# y+ z
TQC 108
  1. public class TQC108 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 for(int i=1;i<=9;i++)
  8.                 {
  9.                         for(int j=1;j<=9;j++)
  10.                         {
  11.                                 System.out.print(String.format("%d*%d=%d\t", i,j,i*j));
  12.                         }
  13.                         System.out.println("");
  14.                 }
  15.         }

  16. }
複製代碼

TOP

回復 3# jerryclass
  w/ v4 y. Q8 m3 m4 \! v; E- U$ J8 b0 L1 Y" m

- I$ g4 R' b9 }: H  ?2 d+ l) U    TQC204
  1. public class TQC204 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 int max = Integer.parseInt(args[0]);
  8.                 int min = Integer.parseInt(args[0]);
  9.                 int passCount = 0;
  10.                 double sum = 0;
  11.                
  12.                 for(int i=0;i<args.length;i++)
  13.                 {
  14.                         int userIn = Integer.parseInt(args[i]);
  15.                        
  16.                         if(userIn > max) max = userIn;
  17.                        
  18.                         if(userIn < min) min = userIn;
  19.                        
  20.                         if(userIn >= 60) passCount++;
  21.                        
  22.                         sum = sum + userIn;
  23.                 }
  24.                
  25.                 System.out.printf("本班最高分數是:%d \r\n", max);
  26.                 System.out.printf("本班最低分數是:%d \r\n", min);
  27.                 System.out.printf("本班及格人數有%d人60分以上 \r\n", passCount);
  28.                 System.out.printf("本班期末考總平均是:%.2f分 \r\n", sum/args.length);
  29.         }

  30. }
複製代碼

TOP

回復 4# jerryclass # e3 U1 `) J$ g! \' v1 u% `

) O6 [# k! w+ }* @- ~* W" d  s+ V
8 D% j6 V5 U. i6 H. ~    TQC 207
  1. public class TQC207 {

  2.         /**
  3.          * @param args
  4.          */
  5.         public static void main(String[] args) {
  6.                 // TODO Auto-generated method stub
  7.                 int max = Integer.parseInt(args[0]);
  8.                 int oddCount = 0;
  9.                 int sum = 0;
  10.                 for(int i=0;i<args.length;i++)
  11.                 {
  12.                         int userIn = Integer.parseInt(args[0]);
  13.                        
  14.                         if(userIn % 2 == 1) oddCount++;
  15.                        
  16.                         sum = sum + userIn;
  17.                 }
  18.                
  19.                 System.out.printf("最大值= %d \r\n",max);
  20.                 System.out.printf("奇數的個數= %d \r\n",oddCount);
  21.                 System.out.printf("數字的總和= %d \r\n",sum);
  22.         }

  23. }
複製代碼

TOP

返回列表