I have this code, which generates me a 2D matrix with one of 4 numbers in the first column and one of 4 numbers in the second column (but both values must not be same). But now, I want to convert the numbers into letters - so instead of the numbers 1, 2, 3 or 4 i wanna have the letters a, b, c or d. I know I have to solve this somehow via the ascii coding, but i don't know how to do it.
And yes, I know, there is exactly this question here in stackoverflow, but I don't know how to implement the method from that question in my class. These 2 lines of code don't help me, because I have this specific code and I'm really new to java or coding in general.
public class scratch{
public static void main(String[] args) {
Number[][] generator = new Number[10][2];
for (int i = 0; i < generator.length; i++) {
for (int j = 0; j < generator[i].length; j++) {
if (j == 0) {
double x = Math.random();
generator[i][j] = x < 0.25 ? 1 : (x < 0.5 ? 2 : (x < 0.75 ? 3 : 4));
} else {
do {
double y = Math.random();
generator[i][j] = y < 0.25 ? 1 : (y < 0.5 ? 2 : (y < 0.75 ? 3 : 4));
} while (generator[i][j].equals(generator[i][0]));
}
System.out.print(generator[i][j] + " ");
}
System.out.println();
}
}
}
char
is actually int
, you can use -
and +
between char
and int
:
// 1 - a; 26 - z
public static char convertToLetter(int num) {
return (char)('a' - 1 + num);
}