Results 1 to 4 of 4
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default 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!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default 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...

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

Similar Threads

  1. Replies: 7
    Last Post: 05-05-2011, 11:45 AM
  2. confusion about the Note class
    By yma16 in forum New To Java
    Replies: 4
    Last Post: 04-16-2011, 06:35 AM
  3. things necessary to take note of before going to learn EE
    By kulangotski in forum New To Java
    Replies: 0
    Last Post: 12-28-2010, 07:30 AM
  4. SCJP Handwritten Note
    By arudra.scjp in forum Java Certification
    Replies: 0
    Last Post: 08-14-2009, 02:24 PM
  5. note while compiling
    By j2vdk in forum New To Java
    Replies: 3
    Last Post: 09-04-2008, 12:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •