How can I assign a specific value to a string without using arrays or enumerations?
Example: 1 for "king", 2 for "queen" 3 for "guard". And then use random generator to generate between 1 and 3.
How can I assign a specific value to a string without using arrays or enumerations?
Example: 1 for "king", 2 for "queen" 3 for "guard". And then use random generator to generate between 1 and 3.
Here is a more complete select case approach:
public static String toName(int value) {
select(value) {
case 1: return "king";
case 2: return "queen";
case 3: return "guard";
}
return null; //no matched value
}
Then borrowing this randomWithRange()
from AusCBloke you can use it like this:
System.out.println(toName(randomWithRange(1,3)));