Erick Vazovsky - 1 year ago 66
Java Question

# how many columns confirm if statement

I need to change :

`System.out.print((i+1) + " ");`
so it doesn't show me which rows confirm if statement, but so it shows me how many rows confirm it.
right now it will say "3 6" but I need it to say "2"

``````    `int A[][] ={{0, 1, 2, 0, 2},
{4, 4, 4, 4, 4},
{0, -1, 8, 10, -1},
{0, 3, -1, 2, 1},
{4, 8, 4, 8, 12},
{-1, -1, 2, 0, 1},
{1, 8, 2, 4, -1},
{8, 16, -1, 4, 0}};`

System.out.println("results: ");
for (i=0; i<8; i++) {
Count = 0;
for (j=0; j<5; j++) {
if (A[i][j] == -1) Count++;
}
if (Count >= 2 )
System.out.print((i+1) + " ");
}
``````

Answer Source

I hope I understood it right. If so your solution would be:

Move the `if` statement to outside the first `for` also your `Count` variable must be outside it

``````int a[][] ={{0, 1, 2, 0, 2},
{4, 4, 4, 4, 4},
{0, -1, 8, 10, -1},
{0, 3, -1, 2, 1},
{4, 8, 4, 8, 12},
{-1, -1, 2, 0, 1},
{1, 8, 2, 4, -1},
{8, 16, -1, 4, 0}};
int count = 0;
for (i=0; i<8; i++) {
int numbersOfMinus1 = 0;
for (j=0; j<5; j++) {
if (a[i][j] == -1){
numbersOfMinus1++;
}
}
if (numbersOfMinus1>=2){
count++;
}
}

System.out.println("results: " + count);
``````

Hint: since you are using Java you should read about the Code Conventions for Java

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download