返回列表 發帖

20120707 上課內容

上課大綱:TQC 考前練習9 X2 a( l8 T" a. E. v/ a, C
練習題目:TQC107、TQC108、TQC204、TQC207

本帖最後由 jerryclass 於 2012-7-7 11:42 編輯
/ E5 R' |& P+ `% g1 N/ m0 M4 p* v$ x' B0 i3 j/ a# t
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
7 u, A! o0 V( J/ p8 M5 ?0 @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   J. B/ V$ S- B6 n1 d

- x1 B# h" m# q3 |, D" o5 ?" k& G9 @2 E! U7 b/ N
    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
' m, R: c; \8 D" n' Y
* {' y: X1 ^$ ^. g1 G! q
9 ]* G: _' D- s, V! {- p* S$ P! N7 R    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

返回列表