这是我的代码的第一部分,它创建了一个随机生成的字母数组,其大小由用户指定。我现在需要做的是从用户接收一个字符串,并测试生成的数组中该字符串的相等性。在测试平等方面,我被困住了。任何帮助将不胜感激。
package arraySearcher;
import java.util.Random;
import java.util.Scanner;
public class arrayGenerator {
// method that gets input from user and creates a random array..
public static void createArray() {
// sets size of array..
System.out.print("How many columns and rows will you have in your square matrix?");
Scanner keyboard = new Scanner(System.in);
int size = keyboard.nextInt();
char[][] userArray = new char[size][size];
// fills array with random letters..
Random randomLetter = new Random();
char c = (char) (randomLetter.nextInt(26) + 'a');
for (int row = 0; row < userArray.length; row++)
for (int column = 0; column < userArray[row].length; column++) {
userArray[row][column] = (char) (randomLetter.nextInt(26) + 'a');
}
System.out.println("Below is your square matrix with randomly generated letters.");
// prints array..
for (int i = 0; i < userArray.length; i++) {
for (int g = 0; g < userArray[i].length; g++) {
System.out.print(userArray[i][g] + " ");
}
System.out.println();
}
}
答案 0 :(得分:0)
你可以尝试这个
char[] ch=new char[]{'a','b','c'};
System.out.println(new String(ch).equals("abc"));
int SIZE = 3;
// row-wise
char[][] rowArray = new char[][] { { 'a', 'b', 'c' }, { 'l', 'm', 'n' }, { 'x', 'y', 'z' } };
System.out.println("check in rows");
for (int i = 0; i < rowArray.length; i++) {
System.out.println(new String(rowArray[i]).equals("abc"));
}
// column-wise
char[][] colArray = new char[SIZE][SIZE];
for (int i = 0; i < colArray.length; i++) {
for (int j = 0; j < rowArray.length; j++) {
colArray[i][j] = rowArray[j][i];
}
}
System.out.println("check in columns");
for (int i = 0; i < colArray.length; i++) {
System.out.println(new String(colArray[i]).equals("bmy"));
}
答案 1 :(得分:0)
鉴于userInput
是您从用户收到的字符串,并且您已将userArray
填充为char
的二维数组,您可以检查{是否{ {1}}以这种方式等于其中一行:
userInput