supun dahanayaka
magicbox
1 Answer(s)      5 months and 18 days ago
Posted in : Java Beginners

How to complete this?

import java.util.*; class Example{ public static void main(String args[]){ Random r=new Random(); int[][] m=new int[3][3];

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            int x=r.nextInt(9)+1;
            System.out.println("ij"+i+" "+j+" "+x);

            boolean xExist=false;

            for(int a=0;a<3;a++){
                for(int b=0;b<3;b++){
                    if(x==m[a][b]){
                        xExist=true;
                    }
                }
            }
            if(!xExist){
                m[i][j]=x;
            }
        }
    }
    //print
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            System.out.println(m[i][j]+" ");
        }
        System.out.println();
    }
}

}

View Answers

December 1, 2012 at 5:53 PM


Here is your required code.

import java.util.*;
class Example2{
    public static void main(String args[]){
        Random r=new Random(); 
        int[][] m=new int[3][3];

    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            int x=r.nextInt(9)+1;
            System.out.println("ij"+i+" "+j+" "+x);

            boolean xExist=false;

            for(int a=0;a<3;a++){
                for(int b=0;b<3;b++){
                    if(x==m[a][b]){
                        xExist=true;
                    }
                }
            }
            if(!xExist){
                m[i][j]=x;
            }
        }
    }
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            System.out.print(m[i][j]+" ");
        }
        System.out.println();
    }
}

}









Related Pages:
Magicbox 2
Magicbox 2  how to build this? import java.util.*; class magicbox{ public static void main(String args[]){ Random r=new Random(); int[][] magic=new int[3][3]; int temp=r.nextInt(9)+1; boolean

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.