返回列表 發帖

20120707 上課內容

上課大綱:TQC 考前練習. I& y6 }/ v; g: l/ p- @
練習題目:TQC107、TQC108、TQC204、TQC207

回復 4# jerryclass + E; j% l: A9 I% {

5 k6 P) L, }0 P8 R: I
1 e: {! x6 ?9 I6 x/ P5 P4 n! q    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

回復 3# jerryclass
9 k" Y( S+ f% w' h' b5 v
5 i2 {/ o. b) f& c2 `( B( d& ?2 x  V1 P
    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

回復 2# jerryclass
2 a3 {5 x7 T) q/ }, |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

本帖最後由 jerryclass 於 2012-7-7 11:42 編輯 : W# f" c3 u9 s, [+ o$ d

( A2 W1 h5 k) k* N8 R- B+ yTQC 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

返回列表