我真的很难在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
答案 0 :(得分:0)
您需要更新字符串中的所有字符。在你的情况下,你只有字符串的第一个字符。
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();
}
}
}
希望这有帮助
答案 1 :(得分:0)
我调整了整个程序,使其更加强大。它现在如何运作:
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();
}
}