简体   繁体   English

在Java中将随机字母放入二维数组中

[英]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. 任何帮助,将不胜感激。

Bring your number inside loop. 将您的number放入循环中。

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

Just move this 只是移动这个

int number = (int) (Math.random() * 26) + 65; 

to just before 到之前

table[r][c] = (char) number;   

then it will get a new value for each loop 那么它将为每个循环获取一个新值

You only generate one character (outside your loop) with a temporary value. 您只能生成一个具有临时值的字符(循环之外)。 Also, I would prefer 'A' to a constant 65 . 另外,我更喜欢'A'而不是常数65 Like, 喜欢,

 for (int r = 0; r < rows; r++){
    for (int c = 0; c < columns; c++){
       table[r][c] = (char) ('A' + (Math.random() * 26));
       System.out.print(table[r][c] + " ");
    }
 }

you are generating a random number only once, here int number = (int) (Math.random() * 26) + 65; 您只生成一次随机数,这里int number = (int) (Math.random() * 26) + 65;

after that, you loop and insert the same number to each location on your 2d-array. 之后,循环并在2D数组的每个位置插入相同的数字。 If int number = (int) (Math.random() * 26) + 65; 如果int number = (int) (Math.random() * 26) + 65; was inside the loop, you would get a random number each iteration, and that way, you would have (theoratically) different numbers. 循环内部 ,您将在每次迭代中获得一个随机数,这样,(理论上)您将拥有不同的数。

So, just change 所以,只要改变

  public static void box(){
 int rows = 10;
 int columns = 10;  
 int number = (int) (Math.random() * 26) + 65;   // move this into loop 
 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

into 进入

  public static void box(){
     int rows = 10;
     int columns = 10;  

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

Also, you can put the two lines togather and remove the need for number local variable: 另外,您可以将两行放在一起并消除对number局部变量的需要:

table[r][c] = (int) (Math.random() * 26) + 65;

Another thing I would have changed is using 'A' instead of 65. It makes it clearer in my opinion... table[r][c] = (int) (Math.random() * 26) + 'A'; 我会改变的另一件事是使用'A'而不是65。在我看来,这变得更清楚... table[r][c] = (int) (Math.random() * 26) + 'A';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM