# Help me

• June 13th, 2010, 06:30 AM
Lakmini Kuruppu
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. :-??

Code :

```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(); } } }```
• June 13th, 2010, 08:31 AM
Norm
Re: Help me
Quote:

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?
Quote:

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.
• June 13th, 2010, 01:42 PM
javapenguin
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
Code :

`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.
• June 13th, 2010, 02:16 PM
javapenguin
Re: Help me
Code :

``` 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.
• June 14th, 2010, 10:32 AM
Faz
Re: Help me
Quote:

Originally Posted by Norm

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

Code :

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

`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?