TQC104# G; z- |) l& p$ I( C' C+ n: N
public class TQC104 {
2 A. w/ F) E' ]$ e) x public static void main(String[] args) {
# t! `) t6 c5 y int a[]={0,1,2,3,4,5,6,7,8,9};
0 `$ @- e! n4 E! P/ E5 ~! m) c1 n boolean check=false;
1 h- }6 Y. Y( W& s; { int i,j,c1,c2,c3,r1,r2,r3,x1,x2;7 ^ T! X/ j$ o! f5 k+ j, \$ z$ R
do{
% o* E5 e: }' v2 ~+ ^3 }( U- ]5 w for( i=1 ; i<a.length ; i++){
# i, h: C+ T" n/ g j=(int)(Math.random()*9)+1;/ t" X, C( F6 X8 j' F
a[0] = a[i];& S# u# q' \( {
a[i] = a[j];
9 R$ m1 ]0 X0 q* Q1 h8 h3 M1 p a[j] = a[0];% A0 ?4 t9 }9 W% I7 p
}5 L( I# f# V1 n' J+ {
' ^5 z& X# ^* I& P" L- x4 [$ D c1=a[1]+a[2]+a[3];
. h( @" O8 p7 _3 V J( R, W c2=a[4]+a[5]+a[6];
5 K! K0 }. ] T7 `2 `6 N c3=a[7]+a[8]+a[9];$ O5 V6 R& a5 Y( _' i# Z: d' K
r1=a[1]+a[4]+a[7];
& g- r1 v1 p( L r2=a[2]+a[5]+a[8];: J% s/ s, e3 s, I8 E* [
r3=a[3]+a[6]+a[9];2 |: N3 F! J' a
x1=a[1]+a[5]+a[9];
8 [9 n+ J- z/ |1 o x2=a[3]+a[5]+a[7];3 @" j2 @4 v: g
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15
2 p4 N6 U5 l2 r3 p9 q && x1==15 && x2==15){
2 I) c8 l( o Y7 k& B check=true;
9 C+ G3 D/ e1 P' U/ f+ R3 h }4 r; {/ B* x* R/ D
}while(check==false);& ~& C; b: \( x" T5 V# @* Q* Z
System.out.println("答案為:");; k B0 t3 w; S- Z5 n, n U5 Y
6 w) g$ W' o% } i3 T5 |8 Y System.out.println(a[1] + " " + a[2] + " " + a[3] );4 J+ w2 x* `; H6 {: X! ~! p
System.out.println(a[4] + " " + a[5] + " " + a[6] );! P- j: I J6 c8 o6 _
System.out.println(a[7] + " " + a[8] + " " + a[9] );
* D( g1 M0 ?8 p4 k# R
5 M" ]* X8 y! x0 [& v; t$ d System.out.println("不論橫向縱向及對角線加起來的和都是:15");; t1 f: c1 F( R+ _7 |
}! b& ~# G" y- x
}5 y" r; w/ y. F; r. ^ B% u
- N# X$ i7 e9 b! s
7 e6 x6 N! r5 p8 j
0 B$ O6 R1 J. f u4 cTQC105
3 [4 i: g: j8 Z+ w9 Uimport java.io.*;0 D, Y! A% w' f* u
public class TQC105 {
: {- }4 u- C! C" O
0 ?( M# \! g l# m4 w public static void main(String[] args){
" X# Y% O2 ~+ ~% k% o try{
. x! J2 U T7 x5 s, m; {$ b int row,col;0 W6 h0 x( X) q A
int i,j;4 R3 c1 m, }# L2 x. t- K! b. B. @6 p
String tmp; p: A6 M N8 `( z" A; m/ ?! @) x
int aryA[][];
# p d3 m. Y# ?; X- e7 ]/ ? int aryB[][];
( B* Y% T& r8 R- Y6 t0 f( i4 V- x BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));
2 T, F1 @1 \! I, F r ( B2 D7 `4 Z& m+ a5 k! w' S
System.out.println("請輸入陣列列數:");- {+ n: i# ^+ ^- q' G7 q; ~; Y* W
row = Integer.parseInt(bin.readLine());% d+ L% ~& m# `8 M' K' @
System.out.println("每一列陣列裡要幾個數字:");8 H: K4 {/ F5 F- N5 V
col = Integer.parseInt(bin.readLine());& T! n2 B5 u+ @1 R7 }
* Y% Z, ]: B; H( F) v6 {% _
aryA = new int[row][col];" c7 w$ O' k h% E. u
aryB = new int[col][row];; y6 d5 S: O7 `: n0 l/ D; l0 o
. P' H, [# W! e) }% L' [
for( i=0 ; i<row ; i++){' m: `' |/ G2 _2 |9 P1 M1 r! C
System.out.println("請輸入第 "+ i +"列");
0 n( ? t5 v0 r8 ?1 _* T$ ]- z/ ] tmp = bin.readLine();- J. U9 {2 D$ c. l7 |- O+ S. i; a
String[] strA = tmp.split(" ");
; J2 f$ o0 _: |. d8 f( V; ^ for( j=0 ; j<col ; j++ ){( R( K$ K5 y; ~/ `: u
aryA[i][j]=Integer.parseInt(strA[j]);
# u4 S3 J3 }- }4 g1 P0 x }& F; i! m* b- T# w ~9 V
}, b% y2 b" o8 d+ Y+ r! J) s
for( i=0 ; i<row ; i++){& x- P* h9 _/ L0 v- T# N1 \
for( j=0 ; j<col ; j++){2 u2 {8 F5 S S2 t! K
aryB[j][i]=aryA[i][j];
1 P# q7 r& r& m- h( G4 C* w }% Z* r( {( Y9 m( i
}
7 j. z0 w! p0 q! r/ w1 d ! c* S: v9 B9 f* _2 n/ H' N
System.out.println("陣列經行列轉換結果");
+ D) W6 O# D2 t2 G3 f1 O8 j3 M for( i=0 ; i<col ; i++){
" ^! t4 O6 i+ m; E/ I for( j=0 ; j<row ; j++){
3 y. d$ L, W2 e3 b; e. q$ v System.out.print(aryA[j][i] + " ");3 `7 ^0 b6 T+ e* A1 N; ~. C/ T3 d2 C+ k
}
" I# q' P; z2 _9 P1 q# ?0 Q System.out.println();
( H# N' V& A- x }& C0 x @7 C8 s7 [: ~, W( u
}catch(Exception e){, B7 K) \* x: ~& x. f
System.out.println("請輸入數字");. T' K3 N& l9 o6 v( H
e.printStackTrace();9 r4 f% J' F; B
} f$ t9 v) M. U
! i+ P. ~9 a* G$ u/ D* Z) V }) D6 A# l( P& b: l2 }
}: i2 W- F' V3 }8 S4 n0 j4 C& n; k
( N0 k& b) A7 M2 z& z) Z2 E
: B5 E: ]/ C( H4 D+ z) h8 B" }TQC106
5 L* M* G4 p) C4 D# y+ x0 o$ x+ Tpublic class TQC106
+ t. [1 p3 ` g& ]0 B+ t0 p{: Z; f }/ h. I& j! y
public static void main(String args[])9 T0 k! |8 Z t8 x0 U* M
{
% H4 y( T H5 Z9 x int sum = 0; 6 Q! I, G c! z3 h
int j = 0;
) P4 ?: _7 F+ h: L' x5 O3 Y7 m9 V int k = 0;8 A7 I# |: ^6 G3 T0 m
for(int i=0 ; i<args.length ; i++){
& u6 O; A0 w' P' u try{* T" n1 ~1 U D* [' m7 h; U
sum += Integer.parseInt(args[i]);
" c% Q9 x f/ ]3 n& S/ \ j++;
3 l/ a9 e) {' g: s; Y' w }
# N: x% h" q- h0 k catch(NumberFormatException e){" c4 w9 V5 E5 Y; C$ }
k++;
" `( K: s: G8 T8 {) a }
. V( J8 |5 w' |8 [; A }
" j6 L" B- g) J" N- _) S System.out.println("數值之總合為:" + sum );1 w# f0 P$ @- w& O' V" l; d+ W
System.out.println("非數值個數為:" + k );
0 ?8 O* m7 _% v3 l9 ^3 a System.out.println("純數值個數為:" + j );# q# ^0 a( T' L$ d7 p( s$ X" \6 R
}
& V% { U) K+ I, H7 r9 s: p. c' a3 D} |