如何在此2D数组中随机添加字母?

时间:2016-05-01 19:58:54

标签: java arrays

我需要使用哪些代码才能在此代码中随机生成一封信?

public class Array {
    public static void main(String[] args) {

    // Create 2-dimensional array.
    int[][] values = new int[5][5];


    // Loop over top-level arrays.
    for (int i = 0; i < values.length; i++) {

        // Loop and display sub-arrays.
        int[] sub = values[i];
        for (int x = 0; x < sub.length; x++) {
        System.out.print(sub[x] + " ");


        }
        System.out.println();

    }

    }
}

1 个答案:

答案 0 :(得分:1)

String s = "abcdefghijklmnopqrstuvwxyz";
//loop through rows
for(int x = 0; x< values[0].length;x++)
{
//loops through columns
   for(int y = 0; y< values.length;y++)
   { 
       int x = (int)(Math.random()*26); // random int between 0-25
       String letter = ""+s.charAt(x); //concatenates
       values[x][y] = letter; // declares.
   }
}

这里是如何在代码中随机生成一封信。