Hi there,
Im new to Java and trying to understand the solution written in the book to solve the placing of the black key's on the piano.
The book wants me to place 2 black, none, 3 black, non, 2 black, non, 1 black key. The array looks like this:
private String blackKeys =
{ "W", "E", "", "T", "Y", "U", "", "O", "P", "", "]" };
I solved it like this:
// make black keys if you find an blank in the array add 2 to the count and so skip a key
public void makeBlackKeys()
{
int y = 0;
while (y < blackKeys.length)
{
if (blackKeys.equals(""))
{
y++;
}
Key key = new Key(blackKeys, blackNotes + ".wav", "black-key.png", "black-key-down.png");
addObject(key, 87 + (y*63), 75);
y = y + 1;
}
}
And it does the trick.
But the book comes up with this:
for(int i = 0; i < whiteKeys.length-1; i++)
{
if( ! blackKeys.equals("") )
{
Key key = new Key(blackKeys, blackNotes+".wav", "black-key.png", "black-key-down.png");
addObject(key, 85 + (i*63), 86);
}
}
I can't seem to understand how 1. the "while" loop now is changed to a "for" loop? 2. It still makes keys when i does not equal "". 2. why deduct -1 and then i++.
Could someone please talk me through the book solution for this?
Dennis

