使用指定帧的 ASCII 字母和数字创建一个随机编码数组,并且 Java 中没有重复
Create a random coding array using the alphabet and numbers from ASCII with a specified frame and no repeats in Java
我在 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;
我不认为这符合您的预期,也不清楚您想要什么。我将重新编写您的代码以生成随机数字或字母,但您可能需要仔细考虑您的代码。
// 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++;
下面解决了 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++; }
根据我的理解,您想从字母和数字中填充一个随机数组以避免重复,那么请尝试下面的操作:
//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]);
}
- 创建一个包含所有字母和数字的字符数组。
- 创建一个列表并使用此数组中的随机元素填充它(如果它们不存在于列表中)。
- 将您的列表转换为数组。
我不确定我是否正确理解了您要执行的操作。但是如果你想要一个包含唯一引用的东西的集合,不要使用列表或数组,使用 HashSet.
我在 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;
我不认为这符合您的预期,也不清楚您想要什么。我将重新编写您的代码以生成随机数字或字母,但您可能需要仔细考虑您的代码。
// 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++;
下面解决了 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++; }
根据我的理解,您想从字母和数字中填充一个随机数组以避免重复,那么请尝试下面的操作:
//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]);
}
- 创建一个包含所有字母和数字的字符数组。
- 创建一个列表并使用此数组中的随机元素填充它(如果它们不存在于列表中)。
- 将您的列表转换为数组。
我不确定我是否正确理解了您要执行的操作。但是如果你想要一个包含唯一引用的东西的集合,不要使用列表或数组,使用 HashSet.