我後來發現你的程式不是陣列的問題,因為你是從0放到9共10個整數,所以你用1到9是沒錯的!!但是你的大括號有問題,我把你的程式修改如下就可以正常運作了,你自己在比對一下囉!
7 X6 C' ?; r, e
1 K. N" @5 N" ?8 u: tpublic class TQC104 {9 z% q0 t' N; T" _1 N& ^0 o* T. B
private static final boolean Expression = false;( F* b5 b# C) J2 D: ^; h% |# L
8 G/ H) Q' H8 ~) l5 ~* }
public static void main(String args [] )0 i0 X8 Y7 _5 N
{
8 Y: W7 {# {( v; ` int a[]={0,1,2,3,4,5,6,7,8,9};# e9 G! M5 x8 d/ a" U
boolean check=false;, P, C8 e& e, L
int i, j, c1, c2, c3, c4, c5, c6, c7, c8;
5 ~" O- ^& D A# a0 g! |9 W do' {$ u9 b) Z* j' ?% f. v
{0 R3 j1 e# Y+ L0 G
for( i=1 ; i<a.length ; i++)
( M/ k- o& j' @, C {
% t1 L6 S5 ]* N6 o( k( } j=(int)(Math.random()*9)+1;- r) H3 D3 j. \( h9 ~
a[0] = a[i];
+ ~3 b4 g+ I+ h7 |1 T a[i] = a[j];
% b9 C7 Y2 D3 J6 u% p a[j] = a[0];" J0 v8 e- @6 E/ }" {) _* L& m8 S
}
7 {, b& Z2 G7 \$ G+ D# Y, U+ n c1 = a[1] + a[2] + a[3];9 o: I$ V! u K" b1 C, K
c2 = a[4] + a[5] + a[6];
/ ]$ v( }0 \ A% { c3 = a[7] + a[8] + a[9];; k8 o: B+ P S9 i2 c0 ~; U
c4 = a[1] + a[4] + a[7];% T1 Y; j, ]4 W" z4 e
c5 = a[2] + a[5] + a[8];0 i6 R$ d7 L" u( Y6 n4 M
c6 = a[3] + a[6] + a[9];
2 Z+ X' C) z, e0 q c7 = a[1] + a[5] + a[9];( N( Q* u6 \+ b; e) Q: y! r: d
c8 = a[3] + a[5] + a[7];# v" ]4 {7 F6 h6 ~2 p c. b
if( c1==15 && c2==15 && c3==15 && c4==15 && c5==15 && c6==15 && c7==15 && c8==15)
D; W A6 s6 _! p/ g6 ~( E {8 i8 h+ U, f& D
check = true;8 G5 c/ y# w @6 r2 g6 P, j. W% \4 }
}
3 a" ^+ d* b1 n8 s9 p% G }while(Expression);
( D5 v1 I- M4 }- r* O
5 B$ ]$ @; g( y System.out.println("answer:");
" x% H; d, U8 [- V) W% N; s System.out.println(a[1] + " " + a[2] + " " + a[3]);& ~- n4 h1 Z& g! f
System.out.println(a[4] + " " + a[5] + " " + a[6]);
, o, f* _0 C! [ System.out.println(a[7] + " " + a[8] + " " + a[9]);) X. u7 L! k+ E- m( w6 v S( d% R; W
System.out.println("不論橫向縱向及對角線加起來的和都是:15");
2 D. m* w' i# M }7 G5 \# ?9 _: d; ^( u+ I. g% S% V7 t
} |