dimanche 22 mars 2020

Horizontal Alignment of two tables in Java

I need to print out the following output:

J   R   F   M   O   M   A   Z   S    1  7  8  6  2  7  3  9  8  1
B   C   X   Y   P   C   S   T   P    1  3  4  6  8  7  2  9  2  3
G   D   C   G   D   B   F   R   T    4  6  7  8  3  1  1  3  0  3 
K   R   Q   L   B   U   A   M   A    3  6  3  1  2  3  1  2  3  1 
X   P   D   T   F   X   T   C   Z    4  3  7  9  0  6  2  6  7  8 
Q   K   G   X   W   E   R   J   V    6  1  3  5  7  6  7  8  9  9     
Q   C   P   D   G   U   L   J   X    1  6  3  1  2  3  1  2  3  1 
Z   J   N   S   V   S   F   H   G    6  3  7  9  0  6  2  6  7  8
P   T   C   D   H   H   R   G   W    9  3  7  9  0  6  2  6  7  8
M   T   P   B   N   E   S   D   Y    0  3  7  9  0  6  2  6  7  8

So using Math.random methods i can generate random digits and random letters...But I can't align them in such a way as in the example above. Here is my code:

public class AlignmentOfRandCharacters {

    static char getRandomChar() {
        char ch = (char) ('A' + (Math.random() * ('Z' - 'A' + 1)));

        return ch;
    }

    static char getRandomDig() {
        char ch = (char) ('0' + (Math.random() * ('9' - '0' + 1)));

        return ch;

    }

    public static void main(String[] args) {
        for (int i = 0; i <= 100; i++) {

            if (i % 10 != 0) {
                System.out.print(getRandomChar() + "  " + getRandomDig());
            } else {
                System.out.println();
            }

        }
    }

}

Here is the output:


J  5E  6J  5J  2R  3N  5Q  5F  8S  3
G  8E  2G  3Q  4V  4I  4I  1Q  1M  3
B  3E  0I  9V  2V  7K  5A  1G  9D  1
N  8S  5R  6U  7U  7E  7G  9G  1S  5
Y  7R  0A  3H  3M  3X  1N  1U  3M  9
T  8Y  0Q  5Q  3C  5H  6V  5R  3R  1
D  0D  8X  6E  4N  8Q  4R  9Y  9J  2
N  8X  0P  8J  7K  9J  3G  9S  9B  0
P  2M  5W  5C  0K  1N  2T  5E  1R  8
E  4C  7E  8E  7T  5A  5S  1B  4P  3

I am absolutely sure that the answer is on the surface but i can't see it. Thanks for your help




Aucun commentaire:

Enregistrer un commentaire