TQC104
L( t. k& Y6 A, R5 T) apublic class TQC104 {
& H2 Q X5 P3 T" s4 T public static void main(String[] args) {4 N7 X0 j8 F! ]
int a[]={0,1,2,3,4,5,6,7,8,9};# P1 f- H j% }: k6 Y: p
boolean check=false;
2 @2 K. \7 |( {5 v int i,j,c1,c2,c3,r1,r2,r3,x1,x2;
' P8 }9 B% X" s do{5 o1 K L" L9 k5 h/ N+ M
for( i=1 ; i<a.length ; i++){
- K" N+ D" h; M j=(int)(Math.random()*9)+1;8 c+ i9 U+ |3 C1 }
a[0] = a[i];2 K5 r+ i, v& W P) g
a[i] = a[j];' } n3 G1 M1 S+ O$ I
a[j] = a[0];
; Z$ X2 {% N( @' M; @* Z }
" Q: ~6 s5 C* _$ A- `( N" T
4 u: |% l; r6 X& G0 Z c1=a[1]+a[2]+a[3];
* N+ M/ j( W. H c2=a[4]+a[5]+a[6];% `5 y+ i; V: {! P/ L9 N
c3=a[7]+a[8]+a[9];" \1 L1 f* X6 {% h8 \' m/ N0 U d/ [
r1=a[1]+a[4]+a[7];4 W. o& [4 ]* ]2 k& ^( W
r2=a[2]+a[5]+a[8];
7 `& c8 X6 t. M5 _- z4 ^ r3=a[3]+a[6]+a[9];
$ [6 Q5 o4 N% a' c* N x1=a[1]+a[5]+a[9]; ?5 u' C; E; P. ^6 w* G! q
x2=a[3]+a[5]+a[7];. c$ A1 T8 F. W3 G2 y0 d' |
if( c1==15 && c2==15 && c3==15 && r1==15 && r2==15 && r3==15! T% p$ g+ [( C8 ]' z1 Y
&& x1==15 && x2==15){
; y1 O3 o0 ]6 v7 D/ c check=true;8 L; z1 _' G ~0 T7 M8 m7 d( W
}
; S0 d# E0 t* J# B1 P }while(check==false);9 b2 L) _" ], m u2 q& z
System.out.println("答案為:");% V0 g8 n1 Y. i' B
# |5 V4 T: \( K) g0 T
System.out.println(a[1] + " " + a[2] + " " + a[3] );
) V a6 {" p0 d: s! T System.out.println(a[4] + " " + a[5] + " " + a[6] );
/ `+ Z5 q4 O) C X# ~* L+ N, i System.out.println(a[7] + " " + a[8] + " " + a[9] );( x7 a Z8 k# `4 ?! x% F2 J4 n! p
7 ?* z- b; P/ i2 A: E+ X% ~! w5 u
System.out.println("不論橫向縱向及對角線加起來的和都是:15");
7 @3 b) ~; k# H% ^1 y2 U2 h }8 e% h& m# a& i7 j- ^8 `& G
}
1 Z* v4 k1 r4 ]; w0 v
3 v% ^1 O! t6 J6 D+ R
1 |/ C4 [8 e+ G# E) H, w3 Y: P+ ^( w3 \5 q
TQC105
/ [( V; F5 D* ?6 mimport java.io.*;
% `4 Q# E2 r) {5 j8 upublic class TQC105 {4 h* v/ p4 a( L3 O# Z+ Y8 f9 ~, L
4 M" X1 S N- Z/ K5 R+ N8 W3 W
public static void main(String[] args){$ C. o6 B7 ?- h& a, D
try{
+ T: g# B- s% u3 U; T int row,col;
2 g. m" Z8 U8 ~9 K: k int i,j;
% e* f) G4 a% O String tmp;1 O, }1 ?6 V/ U. M! |# r
int aryA[][];8 Z3 Q+ b- R* U; X% y! e4 [
int aryB[][];
' W6 _ R1 W; K7 O6 v! } BufferedReader bin = new BufferedReader(new InputStreamReader(System.in));9 c9 h% \$ ^7 r
; e/ o# ]+ H0 t% N+ z2 E& w
System.out.println("請輸入陣列列數:");
8 Q+ j% k9 S1 u( S K2 j7 n row = Integer.parseInt(bin.readLine());
3 |( r0 i. h/ U9 ^; Z5 W( M System.out.println("每一列陣列裡要幾個數字:");' l/ x6 z2 u# ^3 H% D- R
col = Integer.parseInt(bin.readLine());5 R7 c+ g0 C8 Z9 y+ q
) v& i M; t1 x
aryA = new int[row][col];
% j- m6 z I7 D: T+ [ aryB = new int[col][row];
7 @5 w% ] @0 l
3 V* ]5 |" h1 f8 G; } for( i=0 ; i<row ; i++){/ P# ~5 \" ], j7 h0 P" a7 P6 Y: G
System.out.println("請輸入第 "+ i +"列");
9 S! ?7 y, z9 P; `1 k( y tmp = bin.readLine();
7 I4 f( T) o( A1 J: Q' o! E; V String[] strA = tmp.split(" ");& ?& X7 E6 e( P; J0 {2 b
for( j=0 ; j<col ; j++ ){
- w/ u/ Z$ d6 x& V9 k6 c# y: y' v h aryA[i][j]=Integer.parseInt(strA[j]);
; W- I- U* j9 @. u; c6 e }: q5 @. o! f v0 w( t; O4 ^
} k' n! G7 i% M4 s
for( i=0 ; i<row ; i++){
8 L2 x! M# e' u. Y: [ for( j=0 ; j<col ; j++){$ K: i, g4 i1 |5 L5 k P- R
aryB[j][i]=aryA[i][j];
1 K9 P, O# J- G! l }
+ N1 M% u4 B9 s' h8 I( c% K7 d }
: [! F+ {$ B* O3 K4 @! ~, w
8 m4 k) R6 p! H7 u$ K) U" o System.out.println("陣列經行列轉換結果");9 L( f# h1 }4 ?9 k+ R& `; y
for( i=0 ; i<col ; i++){( {! u( \! ?# Z* p
for( j=0 ; j<row ; j++){8 l: `. {) y, |+ L9 K3 F2 g
System.out.print(aryA[j][i] + " ");( B* h: C" n+ z7 i9 X
}
|. r2 ?- V2 r' E! J System.out.println();2 t$ i2 J8 @( [' \2 n7 `
}6 X- m; M3 S% j& M! B
}catch(Exception e){4 x* I2 C. A. E! g
System.out.println("請輸入數字");8 u" c0 G$ ?% [$ t/ K% d( X
e.printStackTrace();
" L. \/ f% L+ l r- x5 q/ L; G% u3 w }
5 j1 n0 J T7 T5 D
2 ?5 T/ |8 L5 m* f6 s. r3 s }
6 U- s: c+ V" Z}
6 v- V0 F6 i5 h# P: `5 J
" I4 f5 y7 M5 Y: z" P, r7 g7 @ . O# q0 {/ T* G& N6 B
TQC1061 M: k0 t& z( d' S/ ^
public class TQC106- z/ ]- ~0 I8 Z$ `
{
& H3 {0 R; s( [- I% I public static void main(String args[])
! ^$ k3 s8 B. S. z9 ? {
/ Q5 C2 @5 o! t, M1 w" r1 ~* R2 t6 ~ int sum = 0; ! a, V1 G0 L( ~6 b
int j = 0;
% n D; Y3 P( z ]' ^8 a int k = 0;0 ~2 F# ]5 k: y* Q; P* X0 H6 {
for(int i=0 ; i<args.length ; i++){1 m! U: E5 w* }$ o& y" R
try{
8 @/ c( i8 k3 g& G sum += Integer.parseInt(args[i]);
0 W1 F# ]" S& F/ S) o j++;
r5 Z) f( G% S" U- T' Z% U5 a }% l% m o& L# N) u
catch(NumberFormatException e){
7 c! p+ Q8 E3 s; l k++;
7 w9 x* }7 n3 h }
! `9 Z4 ^7 b4 I' d2 _! \0 J }
7 M! e1 g( T9 D& A# h8 A System.out.println("數值之總合為:" + sum );1 u2 c7 ~: W0 c2 q2 q; y
System.out.println("非數值個數為:" + k );
- P' A" Y# v/ h# ^# G% z0 g- U System.out.println("純數值個數為:" + j );
: r, ]! i3 L+ h$ p; y }4 k% h" I2 e/ B3 |5 t+ f2 z
} |