TQC104
9 Q5 P. d* E& z5 i: a# `- Opublic class TQC104 {7 W* d6 i7 D; e
public static void main(String[] args) {
# p& x& g& l% f0 F7 m. j* n% G8 { int a[]={0,1,2,3,4,5,6,7,8,9};
( w. z1 W4 p( d" ]3 u+ K boolean check=false;/ |9 m& e4 f p6 Y8 f+ p
int i,j,c1,c2,c3,r1,r2,r3,x1,x2;
+ |2 u) u9 i) _ do{
9 E: j1 ]: F" V+ p3 E- |- T3 B for( i=1 ; i<a.length ; i++){
; Z5 @: T0 T" n0 F j=(int)(Math.random()*9)+1;7 H1 F @4 b) R2 |1 n. {
a[0] = a[i];. }, C" ?- e B) g6 t) o
a[i] = a[j]; j% S7 h* v6 ^* d
a[j] = a[0];
8 O; W' B) B* x& A9 V5 f, E }, I; W/ {4 u# s; x g
, c& |* k. O5 o/ G1 a
c1=a[1]+a[2]+a[3];
* x0 `# [& ?. K c2=a[4]+a[5]+a[6];- v# X H Q4 r' ?! e
c3=a[7]+a[8]+a[9];
6 U& ` m( p9 a6 ^# V; o+ i2 b r1=a[1]+a[4]+a[7];3 j# ~' ]! J1 T% ~* ^* b: f! B* m4 D
r2=a[2]+a[5]+a[8];8 h+ G9 \! D. s4 q! j
r3=a[3]+a[6]+a[9];1 P+ |' k n, i
x1=a[1]+a[5]+a[9];
0 X+ z& g" b0 V2 { x2=a[3]+a[5]+a[7];
% @ r& U* Z7 T' }0 Z" z if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15+ b# p+ `- I+ b" }4 I) J* z1 {; q- I
&& x1==15 && x2==15){0 O' A4 J1 f! {% M' n2 V5 f
check=true;3 D3 }! z. |- t5 R0 t! I- p
}
' |4 S* M: u H0 Y4 Q: S6 p3 h5 J }while(check==false);
/ [& m% m( b5 h* F2 L+ } System.out.println("答案為:");3 L2 L( U$ u) }7 m! n$ {
% S X: {! a X4 Q. p( {# K System.out.println(a[1] + " " + a[2] + " " + a[3] );
0 P9 ^9 H5 ~; ~7 D% I System.out.println(a[4] + " " + a[5] + " " + a[6] );
; g2 k* D0 f3 I" u6 f% }3 e8 k( V System.out.println(a[7] + " " + a[8] + " " + a[9] );
% U J2 X5 y6 |1 W# k
7 r: x n8 F% o) u System.out.println("不論橫向縱向及對角線加起來的和都是:15");: B' h4 _$ Y! B6 P1 g# U* `
}) Q/ Z. T* ~- G& r: E
}+ _( X: v- L7 b2 N# U
" k- Y- R; J- I* K; E ]8 n
9 u. i3 g( E3 x0 W- L! D( `0 w1 W
TQC105
! j' n( y; O- ]. U. ?8 }% @import java.io.*;
+ I4 Q' M% x9 k; Spublic class TQC105 {
$ P; a+ R, e. h! w/ E' b4 ^* \2 K" r* U- _1 R# D
public static void main(String[] args){ Z7 B+ s* ^8 W, t8 Y
try{7 V7 j' q3 X0 d6 q$ t6 u
int row,col;8 }. d. k* X6 c3 u) F/ w
int i,j;' E6 E" t9 |7 A; q- D8 h# w
String tmp;/ s( T7 C: J+ t. @5 y& {7 a
int aryA[][];" V' n& v: Q$ @
int aryB[][];/ Q) x% x E0 m" ^9 g( R3 L
BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
% q# c4 Y0 D1 ^4 K7 L" Q 4 Y3 J8 j5 k4 `5 @; [2 Z" `
System.out.println("請輸入陣列列數:");* P0 A9 p- j2 t; X
row = Integer.parseInt(bin.readLine());
3 ~8 Q- z5 k; y; r/ e( y1 o System.out.println("每一列陣列裡要幾個數字:");
6 P& Z; \( j6 L+ f col = Integer.parseInt(bin.readLine());9 t) q! r% b1 D- B8 n" h- l4 s$ j
2 u' h( U- p/ E8 j
aryA = new int[row][col];
( q; A& L0 K0 \ aryB = new int[col][row];. Y3 r! r$ D# L, P+ d% K1 b
6 J$ @' \# Q- \
for( i=0 ; i<row ; i++){0 f: z$ w- h' r# X. W9 R
System.out.println("請輸入第 "+ i +"列");( ~* X _6 z% J( q
tmp = bin.readLine();- ^4 a- Y& `; k7 l: F3 ~% _
String[] strA = tmp.split(" ");* \* d; a# u$ j- G
for( j=0 ; j<col ; j++ ){
( v( W" {, b: @; c: V8 t! F aryA[i][j]=Integer.parseInt(strA[j]);
8 Y# O$ M, L; ]7 w5 X( ` }
) D/ l; ~3 j+ G6 O) w }+ b. H8 O/ {* z( k9 J
for( i=0 ; i<row ; i++){. e- ?: w* `, O, d3 h4 o2 }
for( j=0 ; j<col ; j++){
8 K, m# g7 D$ N2 w9 ~# q& u( o$ P& T aryB[j][i]=aryA[i][j];
' y4 Z& i; x- i8 Z( s% _ }
% L) @, g4 m7 ? }( o- o& D3 r$ L9 n0 ]) S4 z2 g; F
% U4 q1 G8 D2 f: n2 B1 t4 N0 d" i System.out.println("陣列經行列轉換結果");% W. [ @8 a9 P7 \) I# p
for( i=0 ; i<col ; i++){
- F9 j. o/ J5 j: b5 J7 T. g for( j=0 ; j<row ; j++){1 Q f$ E6 f# t" }- F
System.out.print(aryA[j][i] + " ");
( `0 p' ~/ K; c g }' d' k/ Z& c6 k% @2 B+ I3 Q+ z6 Q
System.out.println();9 `4 m2 J0 V/ d4 W$ {2 S- e' w
}
9 ~7 J* ?/ K2 ?9 s0 o2 D3 o }catch(Exception e){2 [4 Q) v- M4 X' Z: |- b. [( J) [
System.out.println("請輸入數字");
* s& i& v5 ^2 X e.printStackTrace();8 E8 _' k/ o( `# v2 _. L- J
}$ J: Z+ }$ p' |/ u3 z1 N# I1 ^1 `
) n) Z# K" K7 Y
}4 s* C3 e; D/ U( A
}: Z" N. N! B: [) V
' E+ e; [1 f( O
2 o4 M- Z9 s0 \4 nTQC106
5 V" l [( n' \5 d- G2 k- Opublic class TQC106
3 I. Y5 a- ], Y' w: j& j0 D% P{
9 j" n) _5 P, h1 S0 ]0 l4 a6 S public static void main(String args[])
# m- R* D. W* o+ D* e/ W {5 n; ?$ t: N0 s% ~( G) R1 x
int sum = 0;
' Y. K9 K' s2 C' ]4 |. B int j = 0;
4 z% A( g' |6 t6 @! f int k = 0;
, }5 {- A- b( z1 F2 P& c2 f for(int i=0 ; i<args.length ; i++){
1 @% \! S1 k, a, v+ @$ B8 L try{' ~/ v7 G9 _3 e7 q/ ^5 W
sum += Integer.parseInt(args[i]);
& C, B0 x8 [# ~% g j++;
7 }9 |9 a+ r0 {0 K h5 N( |% | } n$ B4 n" @6 D0 s) p+ ^* W: L ^
catch(NumberFormatException e){
& D8 `7 x/ ]1 @" c7 W/ ~3 h5 j/ H k++;
3 H5 x6 h1 j# |3 L }
5 D: W& s: w4 C9 R% ^ }
- J6 i% e% X( ?2 ]$ G System.out.println("數值之總合為:" + sum );
# f! c" n# q3 h1 A r% W* R' g System.out.println("非數值個數為:" + k ); s- y( g- D" A) R" |9 h. ^1 {
System.out.println("純數值個數為:" + j );. }2 T& y' K9 ?3 I8 S5 g
}" m% ]: q% z4 \6 M8 g
} |