[Solved] Boards equals 0 [closed]


int[] numbers = new int[5]; .. System.out.println(numbers[0]); That would print 0 as well. An array of integers without setting a value will have 0 by default.

If you receive x = 480, y = 360, num2 = 2 (we don’t know num1), lets assume num1=1

X[num1-1] = x;
Y[num1-1] = y;

this would be

X[1-1] = x; //X[0] = 480
Y[1-1] = y; //Y[0] = 360

Now…

   while(i <= num2) {
        System.out.println(X[0]+" "+Y[0]+" "+X[1]+" "+Y[1]);
        i++;
    }

Here you are always printing the same values regardless of i, so if you have not yet set a value for those elements then they will always be 0.

There you will print 480, 360 and then 0, 0 since you only have set values for the array in the position 0. The ones in the position 1 are not yet set, 0 by default.

I don’t know what do you mean by: next output equals 0 0 30 360

But maybe you wanted something like this…

...

//If num1 is 1 then it will fill up X[0] and X[1] with x.
X[num1-1] = x;
Y[num1-1] = y;

    X[num1] = x;
    Y[num1] = y;
..
           // Out of bounds risky here, make sure you have enough elements before calling i+1 index.
            System.out.println(X[i]+" "+Y[i]+" "+X[i+1]+" "+Y[i+1]);
...

But make sure you don’t cause an out of bounds.

Now, if you sent num1=2 then

X[2-1] = x; //X[1] = 480
Y[2-1] = y; //Y[1] = 360

There you are not setting the value for the element with index 0, so it will preserve the default value, 0.

1

solved Boards equals 0 [closed]