dimanche 23 avril 2017

putting random alphabetical letters in 2 dimensional array in java

I am suppose to create a 2 dimensional array that contains alphabetical letters that are all capitalized. The goal of the program is basically building a word search puzzle, and I want to fill the 2 dimensional arrays with random alphabetical letters but with the code that I have currently, the table array just fills out completely with one alphabetical letter that is randomly generated. Is there a way to fill the array with random letters randomly?

  import java.util.*;
  public class puzzle {

  public static void main{
     box();
     //other methods

  public static void box(){
     int rows = 10;
     int columns = 10;  
     int number = (int) (Math.random() * 26) + 65;    
     char[][] table = new char [rows][columns];
     for (int r = 0; r < rows; r++){
        for (int c = 0; c < columns; c++){
           table[r][c] = (char) number; 
           System.out.print(table[r][c] + " ");
        }//inner for loop
        System.out.println();
     }//outer for loop

this code is able to print

Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q
Q Q Q Q Q Q Q Q Q Q

or

V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V
V V V V V V V V V V

and I want it to be something like

A G E C H J E Q D Z
G H K E Q E N M E J
H I Z E K H E Q K H
B Y U M G A E K H M
N Q A Y R Y E C U E
Y D W H X S J R S W
I Y O B N M K G D E
A D J R E X C B N G
W R U I G D D G J F
X C V O E W Q A O P

Any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire