TQC104 5 b7 y% k7 E! O6 E: spublic class TQC104 {( A8 l, Y6 Q; v2 `
public static void main(String[] args) {4 @/ ?& A7 r# n; D# X1 m" @
int a[]={0,1,2,3,4,5,6,7,8,9}; 3 o, f2 g% m# T7 q7 K5 A2 \% w boolean check=false; / k! _' g' r# n int i,j,c1,c2,c3,r1,r2,r3,x1,x2; 6 u- p2 M1 `) P, g9 G% S do{; }% l" l) m3 C, B; S( f
for( i=1 ; i<a.length ; i++){8 h3 L( u7 P5 H& I! t' \7 Y
j=(int)(Math.random()*9)+1;" m' s b; R- d; |9 a
a[0] = a[i];( Y% Q$ v! I8 t
a[i] = a[j];$ Y8 X- v8 a+ f
a[j] = a[0]; % o9 ]$ Z$ H* ^5 k* }3 N" } }+ s, G) Q$ U# j/ A4 M
# `8 C) H, R& B: T3 y c1=a[1]+a[2]+a[3]; 2 H8 c6 b& w; l/ a c2=a[4]+a[5]+a[6];0 T& I- M$ p0 x1 P
c3=a[7]+a[8]+a[9]; 5 D* H$ v$ _: O/ K! @) h1 t4 ` r1=a[1]+a[4]+a[7];2 R4 u! X. R" t* y" R
r2=a[2]+a[5]+a[8]; ! F% j w5 c" ?$ P r3=a[3]+a[6]+a[9];" ]* p& m& ^9 M; y" I* B8 S. l
x1=a[1]+a[5]+a[9];% f$ ~5 X1 D" M6 a
x2=a[3]+a[5]+a[7];- H y: \4 [4 q0 B9 \
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15 c" B9 F2 [! ]# B, _1 F& M3 U
&& x1==15 && x2==15){ 8 U# G: ?3 G' o g+ M check=true;* s# K9 {# G* r6 D+ i
}* V+ P+ s; U; T& O% Y& Q9 G
}while(check==false);2 `. m, g& S8 |9 ~6 d" D9 j6 q
System.out.println("答案為:"); 6 z% ^; F% j. j 9 L* f) K _( p. G4 x System.out.println(a[1] + " " + a[2] + " " + a[3] ); - y5 U' u6 w% C- H System.out.println(a[4] + " " + a[5] + " " + a[6] );+ B7 e( ~; W1 C% W
System.out.println(a[7] + " " + a[8] + " " + a[9] );; Y4 b- v: B6 y+ j+ {& ~, t
! Y% p/ A' l8 B3 Z: ~- M6 J/ M" M
System.out.println("不論橫向縱向及對角線加起來的和都是:15"); : i& ?; Q. e7 W' K2 g1 l2 W } 7 Q8 x' a" y0 A1 e; F) ]9 R} 4 m1 p7 [& g' C$ Q) G1 `. s+ p. V* B( {5 f
4 ]4 A9 K8 z& j
C0 ^5 j ]3 A, w4 e. a% uTQC1052 P0 r0 x! z- c1 ]# X2 c, v! v7 u# b
import java.io.*;! E3 M+ g$ i/ O
public class TQC105 {) U: R) S3 P) L: {' M
6 c6 v# N# K% `, [, D
public static void main(String[] args){9 `/ Q# h, |* Y3 m5 I+ D: V
try{ & ?3 ^0 l+ z& J int row,col;: Z9 G; D0 M' S) J
int i,j; " ?- ?# J/ [0 D String tmp;/ U% I2 S/ ^$ {5 R B5 J8 B& Z6 t
int aryA[][]; ) m! l! }7 D o* y; T q int aryB[][]; ) ^. s! B- M; H/ ]0 K BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));% d3 |- f: r! h) P
( Z' S( [. ?- V1 M# T' u1 f& T
System.out.println("請輸入陣列列數:");3 V4 v5 Y: b: V1 }- H
row = Integer.parseInt(bin.readLine());. q! m9 p5 G+ z* I' G
System.out.println("每一列陣列裡要幾個數字:"); & b6 C# f7 d4 d+ c col = Integer.parseInt(bin.readLine());+ \1 c7 O3 v, e) j: x4 Y0 {
( {* S; n; X3 Z O/ M0 r! E7 H
aryA = new int[row][col]; ]& w! H7 c2 {1 k9 w aryB = new int[col][row]; k% R) E1 _9 M) _0 r7 r 8 G! _8 ^3 @- t# z& G2 C9 [ for( i=0 ; i<row ; i++){ R2 S# R2 y) p. T
System.out.println("請輸入第 "+ i +"列"); , X& d! P$ F4 N% |" G( P6 [ tmp = bin.readLine(); 3 P p$ T6 }/ v. g4 m String[] strA = tmp.split(" ");9 s% A' _! V5 m7 t
for( j=0 ; j<col ; j++ ){! a9 c5 \. P# H2 k; ^# `
aryA[i][j]=Integer.parseInt(strA[j]); 7 g/ v6 u, p3 H1 N4 p% l M }1 `( p: j# b5 w: \; {6 }' w
} w0 m/ _9 @, E d
for( i=0 ; i<row ; i++){ 0 u/ F# |2 W3 Y1 w for( j=0 ; j<col ; j++){ 5 W1 \3 s0 i; f: X aryB[j][i]=aryA[i][j];0 ]- L" H$ e, M' A" I! Z1 O9 b3 h
} 1 F* B1 p( ^7 K }" b% o5 S( B1 J ~2 F
( z, i& l3 a4 g. M+ ^% c System.out.println("陣列經行列轉換結果");# Z+ S ?+ D# B6 V$ P2 M9 d$ p* p
for( i=0 ; i<col ; i++){0 a; W9 k- E$ T8 g8 ^) d
for( j=0 ; j<row ; j++){ 8 A% o: A7 a; z& I( m+ X1 g$ z System.out.print(aryA[j][i] + " "); : N- o% N, E. b+ m- Y. K2 [# u: S3 ?# l } |& e2 t# S, Y4 O/ Q; m s1 }
System.out.println(); 6 I4 a; N! e9 O& F$ f6 G } ( U! H. |# u L: z7 h! T! v, x* n }catch(Exception e){! F) X& y3 Y7 g
System.out.println("請輸入數字");, {: Y: a c3 N4 W3 B" O, V z
e.printStackTrace(); 5 J* _* X P- p& O6 s( z } ! S: a$ G/ i) E9 S' a1 D2 h5 T, _) R+ U I3 C
}' A$ G3 i) q9 R4 M. M. |% Q
}0 \& n4 S' Y, i
M2 b a& A3 Y% E& x" M & I. r- G$ o1 G; p# `) V. rTQC1062 G, D3 Z h. ~! o7 Q% l
public class TQC106. C6 Q7 n4 A, z( Y5 K& s
{8 L N1 U3 u! S. Q9 {
public static void main(String args[])2 ?' l) e2 g: ~# Y
{5 f' B: T3 L4 t6 o, @" f: U2 B
int sum = 0; / S" E* ^" W) m \$ G
int j = 0; 2 o- m8 S' e/ L+ n6 p$ Z
int k = 0;0 w* E$ U8 `+ W; ?9 {) i h& S1 V5 B
for(int i=0 ; i<args.length ; i++){' N l: }5 J: T/ b5 w) }& z
try{ 2 a2 p. ]8 s, w# t( y sum += Integer.parseInt(args[i]);) l+ ?5 g& h2 n8 Q& A" d
j++; 9 L- S+ \# j9 c6 z0 n/ U$ J$ C }7 f" c/ {# n! P1 X' K
catch(NumberFormatException e){ 0 z7 z1 d E. Z! x8 B4 Z q4 h k++; - z# `+ F$ m+ x5 l, V( f }5 M* ?7 h4 `! T# k+ R
} % O7 [7 g+ x0 \- |5 c System.out.println("數值之總合為:" + sum );9 D$ s, K' H, X* q
System.out.println("非數值個數為:" + k ); 4 {7 p/ q$ q3 H6 t" b/ n System.out.println("純數值個數為:" + j ); & l0 C. [/ O3 j1 j5 W+ i/ l }) ~2 Z/ x7 _1 q' s: U
}