返回列表 發帖

TQC107 ~ TQC110 答案

TQC107- L! @: F" _2 j7 N! @
public class test{
, u9 H  O6 @- D, j5 c; `) p* O7 r0 M; |3 b6 Z+ {- B. G! ?+ j( C
        public static void main(String args[]){
2 n, I4 U" @% [) |        + h, C2 Z+ z' d* S  F# u% H; O
                //0 1 2 3 ; {* D, o/ ]2 b& {' X
                //1 1 2 3
7 o! t2 k+ X* l9 S2 m2 b5 w                //2 1 2 3) n/ u# P4 x8 P/ G
               
9 O: M/ U+ U8 f: C. v7 H0 F: }3 N                float tot = 0 ;& b" G. j0 o7 q; H. c  y
                for(int i=1;i<args.length;i++){
+ C7 y1 C8 |: B( X                        tot += Float.parseFloat(args[i]);
* F0 Z, E$ v' {7 i, e                }5 C4 l1 y3 H) Y' I9 b7 b, P' m
                tot /= (args.length-1) ;/ y% \0 X+ x) d6 @- P6 ]
               
! |5 u$ A, q$ x1 Q' }1 B0 i                if(args[0].equals("0")){# p9 t% A# s; N2 f! Y3 m: b& X1 A
                        System.out.printf("平均值:%f",tot);
" C( U- e; B; F; {: Y                }else if(args[0].equals("1")){
$ T6 g. j/ C: |+ F  S5 j' E" }                        System.out.printf("平均值:%.1f",tot);$ M, g- V) t1 ^) `, v) V) ^% x! Q" W* x
                }else{
) g: S0 Q# ^# |* r' o( t( l                        System.out.printf("平均值:%.2f",tot);
) g" O0 l* C: D" |, o                }
! @2 A) B7 w0 e' }0 ~1 X- ^3 w               
( U. y7 d0 o  E! ?  f               
& g4 j- w+ K/ Z3 }" O        }. q( H6 |1 j7 ]( g8 q
! U9 `+ c5 @; s+ X
$ w. f+ A, q: I4 V4 m& p
}
- t" r& q2 h0 J* \' mTQC108
! j3 S. Y& W& e  k, ~! f4 s" w; F1 Aimport java.lang.*;- A2 G4 S0 a$ O7 V; {' B* t
# W6 r3 L" J, I0 P
public class TQC108
8 O: M  J/ A" Q{
1 _" }. q+ _1 y9 v  A  public static void main(String[] args)                                         9 |. ?% V: Q3 i9 s% P+ g, m
  {
( V" w: Q: }3 x7 s& ~   for(int i = 1 ; i< 10 ; i++){
3 X* R+ `& P. @1 q1 U      for(int j = 1 ; j <10 ; j++)
& |6 M/ Z3 ~# P. B         System.out.print(""+i+"*"+j+"="+(i*j)+"\t");
( O. F" T- n1 y5 D& F; r      System.out.println();2 f9 I, y2 e# r4 ~/ {5 ~, _' E4 \
   }8 H3 R  U7 J1 [2 G
  }& G2 U7 w. m; _, |9 `
}
3 ?, `4 {% G9 o0 X+ K3 K+ E
) K) }; X0 w. k5 D, Q' N
1 x: u0 O7 w# Y1 S) kTQC1097 j; i/ J/ |( ^( ]# C- c
public class TQC109 {
' L6 T) I5 U& P9 h" w  public static void main(String args[]) {4 z8 F  Z7 {% M* o5 Z
    int r;+ ]% j# L4 p1 L" l
   r=(int)(Math.random()*100+1);
" z, c; b, U5 M! s' [) F# @  c   double area = r*r*Math.PI;
5 F0 k4 W+ y. t, b: g& j8 a% q) L! \   double vol = r*r*r*Math.PI*43;: B- c/ h" r6 X0 e/ t( N: A  \
   System.out.println( "隨機產生的半徑為:" + r );  o7 A' D! }6 Z+ v( Z. M
   System.out.println( "計算後,直徑為:" + (2*r) );
- C3 J' Z% l" ?$ m   System.out.println( "計算後,圓面積為:" + area );
9 k3 Z+ I$ R, W- l* g   System.out.printf( "四捨五入至小數第1位,則圓面積為:%.1f \n\n",area);
( v& g9 [- c" M$ O# {: D# U  p  O   System.out.println( "計算後,圓體積為:" + vol );
1 S! }" O/ Z, \- r& \* z- B4 `! `. h   System.out.printf( "四捨五入至小數第1位,則圓體積為:%.1f \n\n",vol);5 Q' a/ Q/ g# ?: o6 Y; l$ V; k, |
  }
' c, s- Q# U9 m# v}
, U- p5 w: j; N1 }  h$ A9 o" Z  V+ p) o8 t+ j* I) }% v- \8 k+ F

+ d" P) h1 m# K  fTQC110   
/ q6 R, L4 d3 q% rimport java.io.*;  ~- u: G6 S' F: F, q" [/ j. |
import java.util.Date;, J8 d5 p% I, H: V0 U  R* d
' V$ @: h. @$ p! L! ^+ F
    class TQC110{5 ?! G8 b; X. Y  B) K: a2 a
        String items[][];
) c4 M( ~/ @! a4 Y  `+ n+ w1 O        long start, end;
2 O) I; f( }9 ?/ Z0 t" t' Q( \& d        Date d;$ {$ p) ^4 w0 }7 K5 q7 G3 h
        BufferedReader br;
/ a/ I& D7 _: i" X; q! m2 Z5 F
8 w# o* F5 j/ i6 Y# E! ^& q, x        public static void main(String args[]){7 \" v" C: E. K8 H  F% P
            int total = args.length;- g+ s  Y7 a! |
            int pairs = total / 2;
9 e, M6 b5 y2 l0 N3 l# ~) M, B) B            TQC110 tte;& I/ ^4 e5 K- U4 F) [% _* m
            if(total != 0 && pairs != 0)6 Z+ Z* j' b) M0 P! \7 Z3 o2 D2 U
            {" D4 Q7 w3 _' w8 I) S
               //get value from args..- {) h9 y3 L1 E* ^3 n
              String[][] tt = new String[pairs][2];8 |. P: Z. p% P/ r/ Y3 K
              for(int i=0; i<pairs; i+=2){
: N8 V% K) p0 K! m$ ?" Q- X                      tt[i][0] = args[i];
, w6 l8 B$ |- M" ]8 {1 T% V                      tt[i][1] = args[i+1];6 H/ I4 y' z! e, _- i. x
              }
4 e. c9 J% \% U4 N              tte = new TQC110(tt);
5 x- W8 P6 h5 p2 E. P            }else
& ?2 U3 `4 `" R, C5 @& l  I                  tte = new TQC110();
. T4 L+ x5 o& `9 I( [: l            tte.start();
3 C% ^/ t* }; S* D9 ^" k% u5 J        }% u9 q' V  d6 c1 R; o; I' X+ J

# O! L  i& _# C3 Q2 X2 C        TQC110(){/ C3 Q; d7 k1 B
            items = new String[5][2];" L) N/ C" N& L& E3 e: ^
            items[0][0] = "電腦";/ o8 P1 v8 w( t) q* S  _$ ?5 `
            items[0][1] = "computer";* T' I# `7 c- t3 ?/ W9 g
            items[1][0] = "資料庫";5 J: I4 N7 Z* c4 E, ^
            items[1][1] = "database";
( I+ ~2 B# q+ x2 L& E            items[2][0] = "語法";$ ]3 }/ I3 G( Z6 v+ I' D
            items[2][1] = "syntax";
' l* o$ b! f9 y5 b8 H; y# B3 }            items[3][0] = "學校";
0 J; |: R* \- D5 t$ @, q* e            items[3][1] = "school";; j# Y9 L* ^( T( @
            items[4][0] = "假期";
4 ~5 R  X7 ]) }; I            items[4][1] = "vacation";
# {% f) A: T0 E  H5 N. B  O! H. A            //make default value
" l, R* H1 U: g: i            br = new BufferedReader(new InputStreamReader(System.in));" F% ?: z' s9 H& h# L* h/ p
        }
+ P: P7 r, R0 a; K$ N3 b                  
( c, B6 a4 x9 R0 Y; p- M        TQC110(String[][] it){& t' A- y! D/ L2 P9 t$ P9 U
            items = it;  H$ `7 f' N4 U
            br = new BufferedReader(new InputStreamReader(System.in));4 _/ F5 G0 a# q
        }
, M3 ?' p; @7 [# b( l                  
) B2 l4 J; n8 u4 l! w. ]5 f        void start(){
/ S: m% A5 o+ Z- v" |            String ans = "";
* A3 ]4 A) p+ r+ x( f% }            int correct = 0;      // 計算答對題數6 w0 w7 w# b& G% {4 R; \, O
            d = new Date();- Z4 \! P# n  n& t8 F
            start = d.getTime();  // 開始答題時間( G+ u# U9 S8 F9 s; x9 s! s6 N
            System.out.println("請將題目的中文詞彙翻譯成英文單字!");
: b5 ?% c/ v- ]. {5 R2 Q+ C            System.out.println("輸入英文單字答案後請按Enter鍵:");
  c, ?1 F5 c2 S1 [5 l            System.out.println("           ");. u6 m8 C; ]; H, ^& b% q% O
            for(int j = 0; j < items.length ; j++). v" A- Q) C; L! k% x( {+ G
            {5 G9 y$ ?" ]) y- B5 D3 d
                System.out.println("第" + (j + 1) + "題__ " + items[j][0]);
1 X# d( S. |& D# N8 a                try{" k5 y6 s, z4 `# Q- g6 L
                    ans = br.readLine();7 H% c; u" e1 I0 Z" [) d$ A9 g
                }; _; _) Q8 E' i* k
                catch(IOException ioexception){2 t$ E3 S& _( }2 ]; m3 t# X
                    System.out.println(ioexception);
" i1 R" i) i3 C: w6 d                }
( @& ]3 r7 Z4 r                if(ans.equalsIgnoreCase(items[j][1])){% x+ R, x/ @& N8 a: q
                    correct+=1;
; t; ]0 o' I* ?  m5 n# m                    System.out.println("答對了!");* Y+ a  d# o6 m# Z2 B
                    //count correct" o6 ?$ d; N0 k7 Z# g
                    //ouput correct message
. c$ g. Q& n. m6 i                }else{6 N9 y0 N6 X: E3 L3 Z
                                System.out.println("答錯了! 正確答案是:" + items[j][1]);
' G  b2 Q7 G1 D) g  o( _                    //ouput incorrect message/ S: e4 A0 @  F& I: f: }
                }
( x; v* Q1 k* s% d1 X* U: N# Q            }
/ E7 U# ^& v  A) M. R
7 y( M3 s3 A) K1 e; W- D8 h! E            d = new Date();
6 z6 `6 U9 T: b4 i2 ?$ f7 h            end = d.getTime();    // 結束答題時間
4 C' d/ K$ |" D            System.out.print("你使用了" + (end - start) / 1000L + "秒,  在");
6 K# }9 m4 V) _1 p2 v4 s0 M            System.out.println(items.length + "題中答對了" + correct + "題");9 H; z% ?; V0 Z& A3 _! R+ D8 v
        }. k6 u. J) s3 n" X
    }

返回列表