使用ascii中的字母和数字创建一个随机编码数组,该数组具有指定的帧,在java中没有重复

piok6c0g  于 2021-07-03  发布在  Java
关注(0)|答案(4)|浏览(275)

我已经用java编写了四个月了-还是个新手!我想在java中创建一个随机的无重复数组,用一个框架来查找坐标。我已经初始化了我想要在数组中的字符,但是我一直在打印输出中得到停止符、破折号和方括号等。我错过了什么?我不想走图书馆的道路。

final char[] randomSquare = {'C', 'O', 'D', 'I', 'N', 'G'};
// the Grid used that will be randomly filled
char[][] grid;

int A = 65;
int Z = 90;
int num0 = 48;
int num9 = 57;

// create a Random numbers generator
Random ran = new Random();

grid = new char[randomSquare.length][randomSquare.length];

// loop to print randomSquare header for grid array
for (int j = 0; j < 1; j++) {
    System.out.print("   ");
    System.out.print(randomSquare);
}   
System.out.println();

// print randomSquare at position 0 all the way down
for(int i = 0; i < randomSquare.length; i++) {
    // the letter at the beginning of the row
    System.out.print(randomSquare[i] + "  ");

    // the Grid contents for that line
    int index = 0;
    for(int j = 0; j < randomSquare.length; j++) {

        if (A >= 65 && A <= 90){

            index = ran.nextInt(A + Z);
        }

        if (num9 <= 57 && num9 >= 48) {

            index = ran.nextInt(num0 + num9);
        }
        System.out.print("" + (char)index);
    }
    System.out.println();
    index++;
}
return grid;
k4aesqcs

k4aesqcs1#

我不认为这是你期望它做的,也不清楚你想要什么。我将修改您的代码以生成随机数字或字母,但您可能需要仔细考虑您的代码。

// the Grid contents for that line

//perhaps index isn't the best name
int index = 0;
for(int j = 0; j < randomSquare.length; j++) {

    //keep generating new indexe until we get one within desired range
    while (!(index >= 65 && index <= 90 || index <= 57 && index >= 48))
        index = ran.nextInt(Z-num0)+num0; // gives numbers in a range from num0 to Z (48 to 90)

    System.out.print("" + (char)index);
}
System.out.println();

//not sure why this would be necessary
index++;
jei2mxaa

jei2mxaa2#

据我所知,您希望从字母和数字中填充一个随机数组,以避免重复,请尝试以下方法:

//The array containing letters and numbers.
char[] lettersAndNumbers = {'0','1','2','3','4','5','6','7','8','9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U','V','W','X','Y','Z'};

  // the new list to populate
List<String> lc=new ArrayList<String>();
Random ran = new Random();
int i=0;
while(i<20) {
      //Choose a random index for to take an element from the array
    int rand=ran.nextInt(lettersAndNumbers.length);
      //put this element in the list if it doesn't exist
    if(!lc.contains(String.valueOf(lettersAndNumbers[rand]))) {
         lc.add(String.valueOf(lettersAndNumbers[rand]));
         i++;
    }
}   

  //Convert your list to an array
Object[] myArray=lc.toArray();
for (int j=0;j<myArray.length;j++){
    System.out.println(myArray[j]);
}

创建一个包含所有字母和数字的字符数组。
创建一个列表并用这个数组中的随机元素填充它(如果列表中还没有这些元素)。
将列表转换为数组。

xzabzqsa

xzabzqsa3#

我不确定我是否正确理解了你的意图。但是如果你想要一个包含唯一引用的集合,不要使用列表或数组,使用hashset。

flvlnr44

flvlnr444#

下面是用a-z和0-9以外的字符解决你的问题

for(int i = 0; i < randomSquare.length; i++) {
        // the letter at the beginning of the row
        System.out.print(randomSquare[i] + "  ");

        // the Grid contents for that line
        int index = 0;
        for(int j = 0; j < randomSquare.length; j++) {

            if(ran.nextBoolean()){
            if (A >= 65 && A <= 90){

                index = A + ran.nextInt(Z - A);
            }
            }else
            if (num9 <= 57 && num9 >= 48) {

                index = num0 + ran.nextInt(num9 - num0);
            }
            System.out.print("" + (char)index);
        }
        System.out.println();
        index++;    }

    for(int i = 0; i < randomSquare.length; i++) {
        // the letter at the beginning of the row
        System.out.print(randomSquare[i] + "  ");

        // the Grid contents for that line
        int index = 0;
        for(int j = 0; j < randomSquare.length; j++) {

            if(ran.nextBoolean()){
            if (A >= 65 && A <= 90){

                index = A + ran.nextInt(Z - A);
            }
            }else
            if (num9 <= 57 && num9 >= 48) {

                index = num0 + ran.nextInt(num9 - num0);
            }
            System.out.print("" + (char)index);
        }
        System.out.println()

;
        index++;    }

相关问题