将用户输入的单词放在 Java 中的 2D 数组中



>我在尝试在 2d 数组中输入用户输入的单词时遇到了很大的困难。 我的程序需要做的是创建一个单词搜索拼图,提示用户他或她想要找到多少个单词,以及给定的单词是什么。我遇到的问题是我似乎无法将用户的输入放入 2d 数组中。以下是我当前的代码:

public static void generate(){
int rows = 5;
int columns = 5;
char[][] table = new char [rows][columns];
int numberwanted;
System.out.println("Type in the number of words you want to generate: ");
numberwanted = userinput.nextInt();
System.out.println("Type in the words you want to generate: ");
for (int i = 0; i < numberwanted; i++){
String words = userinput.next();
char te = words.charAt(i);
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
table[r][c] = te;
System.out.print(table[r][c] + " ");
}
System.out.println();
}
}// forloop   

输出:

Type in the number of words you want to generate: 
2
Type in the words you want to generate: 
test
t t t t t 
t t t t t 
t t t t t 
t t t t t 
t t t t t 

进球输出:

Type in the number of words you want to generate: 
2
Type in the words you want to generate: 
test
hi
t e s t x          x x t x x
x x x x x    or    x x e x x   and so on... with x's being empty
x h x x x          x x s x x 
x i x x x          x x t x x
x x x x x          x x h i x 

我对整个程序进行了一些调整,使其更加健壮。现在工作原理:

  • 处理table的每一行。
  • 如果用户想要提供一个单词,请请求该单词。
  • 将单词(可能被截断)添加到当前row
  • 如果需要,用x右填充该行。

最后,渲染整个table

private static int ROWS = 5;
private static int COLUMNS = 5;
public static void main(String[] args) {
char[][] table = new char[ROWS][COLUMNS];
int numberwanted;
try (Scanner in = new Scanner(System.in)) {
System.out.println("Type in the number of words you want to generate: ");
numberwanted = Math.min(in.nextInt(), ROWS);
for (int row = 0; row < ROWS; row++) {
String word = null;
if (row < numberwanted) {
System.out.println(String.format("Type in the %d. word: ", row));
word = in.next();
}
for (int col = 0; col < COLUMNS; col++) {
table[row][col] = word != null && word.length() > col ? word.charAt(col) : 'x';
}
}
}
renderGrid(table);
}
private static void renderGrid(char[][] grid) {
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLUMNS; col++) {
System.out.print(grid[row][col] + " ");
}
System.out.println();
}
}

您需要更新字符串中的所有字符。在您的情况下,您只有字符串的第一个字符。

public static void generate(){
int rows = 5;
int columns = 5;
char[][] table = new char [rows][columns];
int numberwanted;
Scanner sc = new Scanner(System.in);
System.out.println("Type in the number of words you want to generate: ");
numberwanted = sc.nextInt();
System.out.println("Type in the words you want to generate: ");
int j=0;
for (int i = 0; i < numberwanted; i++){
String words = sc.next();
char te = ' ';
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if(j<words.length()) te = words.charAt(j++);
else te = '*';
table[r][c] = te;
System.out.print(table[r][c] + " ");
}
System.out.println();
}
}
}

更新。 此外,如果您只需要对角线元素。就是这样

public static void generate(){
int rows = 5;
int columns = 5;
char[][] table = new char [rows][columns];
int numberwanted;
Scanner sc = new Scanner(System.in);
System.out.println("Type in the number of words you want to generate: ");
numberwanted = sc.nextInt();
System.out.println("Type in the words you want to generate: ");
int j=0;
for (int i = 0; i < numberwanted; i++){
String words = sc.next();
char te = ' ';
for(int r = 0; r < rows; r++){
for(int c = 0; c < columns; c++){
if(j<words.length() && r==c) te = words.charAt(j++);
else te = '*';
table[r][c] = te;
System.out.print(table[r][c] + " ");
}
System.out.println();
}
}
}

希望这有帮助

相关内容

  • 没有找到相关文章

最新更新