# getting the note of the number

Printable View

• 09-20-2011, 11:46 PM
droidus
getting the note of the number
on a piano, if a user enters a number -44 to 44, my program has to be able to tell them what key it is. for example, 0 is a. if they enter 13, they should get a#. i was struggling to come up with a formula that simplifies the number down to one b/w 1-12. any help is much appreciated!
• 09-20-2011, 11:51 PM
Fubarable
Re: getting the note of the number
As per usual, why not show us first what you've tried and tell us how it's not working.
• 09-21-2011, 01:15 AM
droidus
Re: getting the note of the number
think i got it:

int n = 48;
String note = "";

while (n>12) {
n -= 12;
}

switch (n) {
case 1: note = "A"; break;
case 2: note = "A#"; break;
case 3: note = "B"; break;
case 4: note = "C"; break;
case 5: note = "C#"; break;
case 6: note = "D"; break;
case 7: note = "D#"; break;
case 8: note = "E"; break;
case 9: note = "F"; break;
case 10: note = "F#"; break;
case 11: note = "G"; break;
case 12: note = "G#"; break;
}

System.out.println(note);

seems to work fine...
• 09-21-2011, 01:27 AM
Fubarable
Re: getting the note of the number
Another possible solution: put the Strings in an array, and use the mod operator and simple algebra to change the number into the index in the array to the correct note.