以下為上課範例,請各位貼上來自己打的- public class test{
- public static void main(String args[]){
-
- int n[] = {0,1,2,3,4,5,6,7,8,9} ;
-
- while(true){
-
- for(int i=1;i<=9;i++){
- int r = (int)(Math.random()*9)+1 ;
- int t = n[i] ;
- n[i] = n[r] ;
- n[r] = t ;
- }
-
- int a1 = n[1]+n[2]+n[3];
- int a2 = n[4]+n[5]+n[6];
- int a3 = n[7]+n[8]+n[9];
- int a4 = n[1]+n[4]+n[7];
- int a5 = n[2]+n[5]+n[8];
- int a6 = n[3]+n[6]+n[9];
- int a7 = n[1]+n[5]+n[9];
- int a8 = n[3]+n[5]+n[7];
-
- if(a1==15&&a2==15&&a3==15&&a4==15&&a5==15&&a6==15&&a7==15&&a8==15){
- break;
- }
- }
-
- System.out.printf("%d\t%d\t%d\n",n[1],n[2],n[3]);
- System.out.printf("%d\t%d\t%d\n",n[4],n[5],n[6]);
- System.out.printf("%d\t%d\t%d\n",n[7],n[8],n[9]);
-
- }
- }
複製代碼 |