mardi 1 mars 2016

How do I generate A-Z randomly to fill an array?

My assignment is to create a 3 part program which when you press 1, it runs method or "feature" 1, when you press 2, it runs feature 2, press 3, it runs feature 3. The end. I have the second feature figured out, but this feature (feature 1) is supposed to generate A-Z randomly to fill a 10x20 array. The code AS IS generates random integers and correctly fills and DISPLAYS a 10 x 20 grid. HOWEVER, how do I get it to display A-Z characters instead of numbers?

public class ModuleOnefor8 {

    public static void main(String[] args) {
        int[][] matrix = new int[10][20];

        for (int i=0; i<matrix.length; i++) {
            for (int j = 0; j<matrix[i].length; j++) {
                matrix[i][j] = (int)(Math.random()*'Z'-'A' + 1);

                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire