Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Help me

  1. #1
    Junior Member
    Join Date
    May 2010
    Location
    Ratnapura,Sri Lanka
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help me

    I tried to create a box(3*3) and addition of horizontal,vertical and diagonal value should be equal to 15.
    But Random class generate 0 and minus values and the program does not work as I expected.Please help me to correct my program.

    import java.util.*;
    class mb {
    	public static void main(String args[]){
    		int ran[][]=new int[3][3];
    		boolean H[]=new boolean[3];
    		boolean V[]=new boolean[3];
    		boolean HV[]=new boolean[2];
    		final int size =3;
    		Random r=new Random();
    		boolean Equal;		
     
    			boolean s_con=true;
    			for(int i=0;i<size; i++){
    				for(int j=0; j<size; j++)
    					ran[i][j]=0;	
    			}						
    				M1:for(int i=0; i<size; i++){
    					M2:for(int j=0; j<size; j++){					
    						int x=r.nextInt();
    						x=x%9+1;
    						boolean condition=true;		
    						L1:for(int n=0; n<size; n++){
    							L2:for(int m=0; m<size; m++){
    								if(x==ran[n][m]){
    									condition=false;
    									break L1;
    								}
    							}	
    						if(condition)
    							ran[i][j]=x;
    						if(!condition)						
    							break M1;
    					}			
    				}	
    		H[0]=ran[0][0]+ran[0][1]+ran[0][2]==15;
    		H[1]=ran[1][0]+ran[1][1]+ran[1][2]==15;
    		H[2]=ran[2][0]+ran[2][1]+ran[2][2]==15;
     
    		V[0]=ran[0][0]+ran[1][0]+ran[2][0]==15;
    		V[1]=ran[0][1]+ran[1][1]+ran[2][1]==15;
    		V[2]=ran[0][2]+ran[1][2]+ran[2][2]==15;
    		HV[0]=ran[0][0]+ran[1][1]+ran[2][2]==15;
    		HV[1]=ran[2][0]+ran[1][1]+ran[0][2]==15;
     
    		Equal=(H[0]&&H[1]&&H[2]&&V[0]&&V[1]&&V[2]&&HV[0]&&HV[1]);
    		}		
    		for(int i=0; i<size;i++){
    			System.out.print("\t");
    				for(int j=0; j<size; j++)
    					System.out.print(ran[i][j]+" ");		
    				System.out.println();
    			}
    	}
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help me

    Random class generate 0 and minus values
    Yes nextInt() will return 0s. I don't know about minus values. Can you show this happening in your code by adding println() statements to print them out?
    program does not work as I expected
    No, they often don't. The computer does what you tell it to, not what you expect it to.
    Can you explain what the program does that you don't expect and what you want it to do.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help me

    Your values for ran are changing every time it's run.

    What is s_con ever used for?

    Also, why is equal being set to
    H[0]&&H[1]&&H[2]&&V[0]&&V[1]&&V[2]&&HV[0]&&HV[1]);

    I thought booleans were only set to true or false.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Help me

     import java.util.*;
        class mb {
           public static void main(String args[]){
             int ran[][]=new int[3][3];
             boolean H[]=new boolean[3];
             boolean V[]=new boolean[3];
             boolean HV[]=new boolean[2];
             final int size =3;
             Random r=new Random();
             boolean Equal;		
     
             boolean s_con=true;
             for(int i=0;i<size; i++){
                for(int j=0; j<size; j++)
                   ran[i][j]=0;	
             }						
          		M1:
             for(int i=0; i<size; i++){
             		M2:
                for(int j=0; j<size; j++){					
                   int x=r.nextInt();
                   x=x%9+1;
    					System.out.println(x);
                   boolean condition=true;		
                		L1:
                   for(int n=0; n<size; n++){
                   		L2:
                      for(int m=0; m<size; m++){
                         if(x==ran[n][m]){
                            condition=false;
                            break L1;
                         }
                      }	
                      if(condition)
                        {
    						   ran[i][j]=x;
    							System.out.println(ran[i][j]); // isn't outputting same number of numbers for each of the three times
    							}
                      if(!condition)						
                         break M1;
                   }			
                }	
                H[0]=ran[0][0]+ran[0][1]+ran[0][2]==15;
                System.out.println(H[0]);  // usually false
                H[1]=ran[1][0]+ran[1][1]+ran[1][2]==15;
                System.out.println(H[1]); // usually false
                H[2]=ran[2][0]+ran[2][1]+ran[2][2]==15;
                System.out.println(H[2]); // usually false
     
                V[0]=ran[0][0]+ran[1][0]+ran[2][0]==15;
                System.out.println(V[0]); // usually false
                V[1]=ran[0][1]+ran[1][1]+ran[2][1]==15;
                System.out.println(V[1]);  // usually false
                V[2]=ran[0][2]+ran[1][2]+ran[2][2]==15;
                System.out.println(V[2]); // usually false
                HV[0]=ran[0][0]+ran[1][1]+ran[2][2]==15;
                System.out.println(HV[0]); // usually false
                HV[1]=ran[2][0]+ran[1][1]+ran[0][2]==15;
                System.out.println(HV[1]); //usually false
     
                Equal=(H[0]&&H[1]&&H[2]&&V[0]&&V[1]&&V[2]&&HV[0]&&HV[1]);
                System.out.println("Equal is: " + Equal); // Equal is always false
             }		
             for(int i=0; i<size;i++){
                System.out.print("\t");
                for(int j=0; j<size; j++)
                   System.out.print(ran[i][j]+" ");		
                System.out.println();
             }
          }
       }

    It needs a condition to make sure none of the values can be less than 0.
    I noticed all my println statements were executing three times. Any particular reason why the code is setting it to do that?
    What's the "\t" for? I'm not familiar with that one.
    Last edited by javapenguin; June 13th, 2010 at 01:23 PM.

  5. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default Re: Help me

    Quote Originally Posted by Norm View Post

    No, they often don't. The computer does what you tell it to, not what you expect it to.
    Can you explain what the program does that you don't expect and what you want it to do.
    Haha you said it.

    OK the nextInt needs to be given a range I don't know what range it is in your case it could be 1-9, 1-15, 0-15 I don't believe you said. I'm guessing from your code (x=x%9+1) it's 1-9 so lets assume that for this post.

    you would need 9 numbers I don't think you can set a range(I may be wrong here) so you just add the minimum number you'd expect so in this case 1 so you would say

    int x=r.nextInt(9);
    x+=1;
    or
    int x=r.nextInt(9) + 1;

    That should give you just numbers in the range 1-9 you can adapt this obviously.

    But I'm not really sure what you want it to do are you checking if the lines add up to 15 or are you trying to ensure they add up 15?