java - "Cycle" through specific ASCII code-range -
i supposed create programme in java takes upper case letter user, subtracts 2 ascii code , outputs resulting letter. clue supposed output letters a-z meaning after z starts 1 time again @ a. best way this? obvious way remove 26 if value reached seems inappropriate.
hope can help me. in advance!
chars in java can treated numbers , can perform ordinary arithmetic on them. byte value 'a' equivalent 65 (i.e. in ascii table, capital 65th ascii table, capital b 66, etc.). wrote simple code steps required broken down:
letter = (letter - 65); letter = (letter - 2); if (letter < 0) letter = letter + 26; letter = letter + 65;
the first line subtracts position of 'a' in ascii table. means 'a' becomes 0, 'b' becomes 1, etc. next line subtracts 2 , next line ensures "loops back" end of alphabet if subtraction caused become negative. final line adds 65 letter, returning capital letter in ascii table.
java char ascii
No comments:
Post a Comment