dimanche 25 janvier 2015

How to consolidate if statements

Here is a section of code:



if (y[X][Y] == 'B')
{
System.out.printf(" (%2d,%2d) %3d\n", X, Y, digCount);
boneFound = true;
}
else
if (y[X][Y + 1] == 'B')
{
System.out.printf(" (%2d,%2d) %3d\n", X, Y + 1, digCount);
boneFound = true;
}
else
if (y[X][Y - 1] == 'B')
{
System.out.printf(" (%2d,%2d) %3d\n", X, Y - 1, digCount);
boneFound = true;
}
else
if (y[X - 1][Y] == 'B')
{
System.out.printf(" (%2d,%2d) %3d\n", X - 1, Y, digCount);
boneFound = true;
}
else
if (y[X + 1][Y] == 'B')
{
System.out.printf(" (%2d,%2d) %3d\n", X + 1, Y, digCount);
boneFound = true;


I want to search an index of a double array as well as each index that is one space up, down, left, and right. Then I want to print the index of the array that has the character 'B'. Is there a more effective way of doing this without all of these if statements?


Aucun commentaire:

Enregistrer un commentaire