Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


supun dahanayaka
magicbox
1 Answer(s)      a year and 4 months 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();
    }
}

}


Advertisement
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 Tutorials/Questions & Answers:
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
Advertisements
Advertisements
 

 

 

DMCA.com