Java 混合一系列字母
在我的作业中,我们得到了一个程序,该程序使用字母表的前四个字母创建一个4x4数组。数组已排序以开始。我已经完成了所有的工作,除了任务中的混乱部分。扰码是从构造函数调用的,而不是从主方法调用的,它应该会混淆数组中的字母。到目前为止,我已经尝试从集合中洗牌,但我发现它用于整数,而不是字符值。我也尝试过将数组转换为列表,但我不知道如何将列表放回数组中。如何混合数组中的字母Java 混合一系列字母,java,arrays,Java,Arrays,在我的作业中,我们得到了一个程序,该程序使用字母表的前四个字母创建一个4x4数组。数组已排序以开始。我已经完成了所有的工作,除了任务中的混乱部分。扰码是从构造函数调用的,而不是从主方法调用的,它应该会混淆数组中的字母。到目前为止,我已经尝试从集合中洗牌,但我发现它用于整数,而不是字符值。我也尝试过将数组转换为列表,但我不知道如何将列表放回数组中。如何混合数组中的字母 public LetterPuzzle() { puzzle = new char[4][4]; letters
public LetterPuzzle() {
puzzle = new char[4][4];
letters = new char[4];
for (int x=0; x<letters.length; x++) {
letters = makeRow();
puzzle[x] = letters;
}
System.out.println("Original: " + puzzle);
scramble();
}
private char[] makeRow()
{
char [] let = new char[4];
for (char c='A'; c<'E'; c++)
let[(int)(c-'A')] = c;
return let;
}
/**
* Represents puzzle as 4x4 matrix with row & column headings
* @return puzzle as matrix String
*/
public String toString() {
String s = " 0 1 2 3\n";
s = s + "--------------\n";
for (int x=0; x<puzzle.length; x++) {
s = s + x + "|";
for (int y=0; y<puzzle.length; y++) {
s = s + " " + puzzle[x][y]+ " ";
}
s = s + "\n";
}
return s;
}
/******TO BE COMPLETED BY STUDENT*****/
/**
* Checks for duplicates in a row
* @param row is the row to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkRow(int row) {
for (row = 0; row < puzzle.length; row++){
for(int j = row + 1 ; j < puzzle.length; j++) {
if( puzzle[row].equals(puzzle[j])) {
return false;
}
else if(row > 0) {
throw new IllegalArgumentException("Row is out of bounds");
}
return true;
}
}
return true;
}
/**
* Checks for duplicates in a column
* @param col is the column to be checked: must be a value 0 .. puzzle.length
* @return false if there are duplicates, true if not
* @throws IllegalArgumentException if row is out of bounds
*/
public boolean checkColumn(int col) {
for(col = 0; col < puzzle.length; col++){
for(int j = col + 1; j < puzzle.length; j++) {
if( puzzle[col].equals(puzzle[j])) {
return false;
}
else if(col > 0) {
throw new IllegalArgumentException("Column out of bounds");
}
}
}
return true;
}
/**
* Scrambles puzzle so that letters appear in random order
*/
void scramble( ){
}
public static void main (String [] args) {
LetterPuzzle puzz = new LetterPuzzle();
System.out.println(puzz);
// Add code here to test your methods & display results
}
公共字母拼图(){
拼图=新字符[4][4];
字母=新字符[4];
对于(intx=0;x尝试为每个新数组索引获取一个随机位置
void scramble () {
puzzle = new char[4][4];
scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
something
letters = new char[4];
int letterIndex = 0;
for (int x=0; x<letters.length; x++) {
for (int y=0; y<letters.length; y++) {
for( true ) {
Random rand = new Random();
int upperbound = letter.length;
int index = rand.nextInt(upperbound) ; //get a random position for current letter
if(scrambledPuzzle[x][index] == ''){ // not yet filled position
scrambledPuzzle[x][index] = letters[y]; // place it here
break;
}
}
}
}
}
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
无效置乱(){
拼图=新字符[4][4];
scrambledPuzzle=newchar[4][4];//用空字符或
某物
字母=新字符[4];
int-letterIndex=0;
对于(intx=0;x尝试为每个新数组索引获取一个随机位置
void scramble () {
puzzle = new char[4][4];
scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
something
letters = new char[4];
int letterIndex = 0;
for (int x=0; x<letters.length; x++) {
for (int y=0; y<letters.length; y++) {
for( true ) {
Random rand = new Random();
int upperbound = letter.length;
int index = rand.nextInt(upperbound) ; //get a random position for current letter
if(scrambledPuzzle[x][index] == ''){ // not yet filled position
scrambledPuzzle[x][index] = letters[y]; // place it here
break;
}
}
}
}
}
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
无效置乱(){
拼图=新字符[4][4];
scrambledPuzzle=newchar[4][4];//用空字符或
某物
字母=新字符[4];
int-letterIndex=0;
对于(int x=0;x如果使用字符
包装类来存储字符,则可以按如下方式轻松地将其洗牌:
Character[] chars =
{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
Collections.shuffle(Arrays.asList(chars));
System.out.println(Arrays.toString(chars));
印刷品
[g, b, d, a, c, i, f, e, h]
如果你想使用基本数组,你可以像这样洗牌它们
void scramble () {
puzzle = new char[4][4];
scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
something
letters = new char[4];
int letterIndex = 0;
for (int x=0; x<letters.length; x++) {
for (int y=0; y<letters.length; y++) {
for( true ) {
Random rand = new Random();
int upperbound = letter.length;
int index = rand.nextInt(upperbound) ; //get a random position for current letter
if(scrambledPuzzle[x][index] == ''){ // not yet filled position
scrambledPuzzle[x][index] = letters[y]; // place it here
break;
}
}
}
}
}
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
- 在
0
和阵列长度-1
之间生成索引next
- 用
chars[i]
交换chars[next]
chars[next]
现在已永久修复
- 重复此过程,在
0
和arrayLength-2
之间生成索引
- 当
i==0
时,就完成了
如果使用字符
包装类来存储字符,则可以按如下方式轻松地对其进行洗牌:
Character[] chars =
{ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i' };
Collections.shuffle(Arrays.asList(chars));
System.out.println(Arrays.toString(chars));
印刷品
[g, b, d, a, c, i, f, e, h]
如果你想使用基本数组,你可以像这样洗牌它们
void scramble () {
puzzle = new char[4][4];
scrambledPuzzle = new char[4][4]; // fill all of this with empty char or
something
letters = new char[4];
int letterIndex = 0;
for (int x=0; x<letters.length; x++) {
for (int y=0; y<letters.length; y++) {
for( true ) {
Random rand = new Random();
int upperbound = letter.length;
int index = rand.nextInt(upperbound) ; //get a random position for current letter
if(scrambledPuzzle[x][index] == ''){ // not yet filled position
scrambledPuzzle[x][index] = letters[y]; // place it here
break;
}
}
}
}
}
char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'};
- 在
0
和阵列长度-1
之间生成索引next
- 用
chars[i]
交换chars[next]
chars[next]
现在已永久修复
- 重复此过程,在
0
和arrayLength-2
之间生成索引
- 当
i==0
时,就完成了
1.使用随机数生成器拾取索引2.将其中一个索引保存到一个变量,然后用第二个索引覆盖该索引。3.然后用变量替换第二个索引。您在其中有一些代码存根,用于检查行/列中是否存在重复项。无序排列后,行/列中是否不允许存在任何重复项?@Idle_注意:洗牌后,代码会检查每行中是否有重复的字母,如果发现重复的字母,它会抛出异常并返回false。最终目标是让它洗牌字母,这样就不会有重复的字母。那么,你是否应该使用无限循环,洗牌并检查,直到你遇到一个通过的组合?你认为呢FFL仅在每行?…或任何位置(行和列)的字符中填充字符?我想你想检查行和列是否没有重复项?@Idle\u注意:是的,无限循环可能在达到正确的组合之前工作得最好,尽管我们从未被告知必须做任何特定的事情。行和列的字符也要检查重复项。1.使用随机数生成器选择索引2.将其中一个索引保存到一个变量,然后用第二个索引覆盖该索引。3.然后用变量替换第二个索引。您在其中有一些代码存根,用于检查行/列中是否存在重复项。洗牌后,行/列中是否不允许有任何重复项?@Idle\u注意:洗牌后,代码ecks检查每行中的任何重复字母,如果发现重复,它将抛出异常并返回false。最终目标是让它洗牌字母,这样就不会有重复的字母。那么,您是否应该使用无限循环,洗牌并检查,直到出现通过的组合?您是否只洗牌每行中的字符?…或来自任何位置的字符(行和列)?我想你想检查行和列是否没有重复?@Idle\u注意:是的,无限循环可能在达到正确的组合之前工作得最好,尽管我们从未被告知必须做任何特定的事情。行和列的字符也要检查重复。