There are 3 different chars, e.g. “a”, “b” and “c”. I need to assign the value “a” to indexes 1, 4, 7, etc., the value “b” to 2, 5, 8, etc, and the value “c” to 3, 6, 9, etc. Now imagine that the index is equal to 11. I should define the char to which it corresponds. How can I do this in JAVA? PS. I need it for the Switch-statement.
Update#1:
I thought that maybe possible solution could be the following: Take the given number, e.g. 11. Divide it by 3, because there are 3 possible choices, i.e. “a”, “b” and “c”. 11/3 = 3.6(6). And then if the whole part can be divided by 3 without the remainder, then estimate how may 3s it includes. In our exaple it’s 1. And finally 1+round(0.66) = 2. So, the value “b” should be selected. But this solution requires using the recursion.
Use Java’s modulo operator:
Compacted with
returns for brevity. In real life pleasebreak;between independent cases.