java - 如何根据用户输入将任何内容显示到数组中?
问题描述
基本上我必须为我的班级制作一个随机单词搜索生成器,我可以获取用户输入来创建单词搜索将有多少行和多少列,我可以让我的程序要求用户输入 x 个单词查找(基于行)和单词的长度(基于列)但是对于我的生活,我无法找到如何在我制作的这个神秘网格中打印任何内容。我只需要找出如何在数组/网格/板中添加随机字母。谢谢。
import java.util.*;
public class Assignment2 {
Scanner keyboard = new Scanner(System.in);
private int[][] wordBoard;
private char[][] gameBoard;
private String[] wordList;
private static int row;
private static int col;
Random rnd = new Random();
public void playWordSearch() {
wordBoard = numRowCol();
wordStorage(wordBoard);
}
/**
* This method asks the user to input an integer for Rows/Columns
* And gracefully handles invalid input :)
*
* @return
*/
public int[][] numRowCol() {
int array[] = new int[100];
boolean again = true;
while (again) {
for (int i = 0; i <= 1; i++) {
try {
if (i == 0) {
System.out.printf("Enter a number of rows (2-15): %n");
array[i] = keyboard.nextInt();
if (array[i] > 15 || array[i] < 2) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
}
}
} catch (InputMismatchException e) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
keyboard.next();
e.printStackTrace();
}
try {
if (i == 1) {
System.out.printf("Enter a number of columns (2-15): %n");
array[i] = keyboard.nextInt();
if (array[i] > 15 || array[i] < 2) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
}
}
} catch (InputMismatchException e) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
keyboard.next();
e.printStackTrace();
}
}
again = false;
}
System.out.printf("The amount of rows you have entered: %d%nAnd the amount of columns: %d%n", array[0], array[1]);
int[][] arrayArray = new int[array[0]][array[1]];
return arrayArray;
}
public int wordStorage(int[][] arrayArray) {
int size;
String word[] = new String[arrayArray.length];
int sizeRow = arrayArray.length;
int sizeCol = arrayArray[1].length;
System.out.printf("rows: %d%n", sizeRow);
System.out.printf("cols: %d%n", sizeCol);
for (int i = 0; i < arrayArray.length; i++) {
System.out.printf("Enter a word with less than %d characters: %n", sizeCol);
word[i] = keyboard.next();
}
keyboard.close();
System.out.println(Arrays.deepToString(arrayArray).replace("], ",
"]\n").replace("[[", "[").replace("]]", "]"));
return 100;
}
}
解决方案
import java.util.*;
public class Assignment2 {
Scanner keyboard = new Scanner(System.in);
private char[][] wordBoard;
private char[][] gameBoard;
private String[] wordList;
private static int row;
private static int col;
Random rnd = new Random();
public void playWordSearch() {
wordBoard = numRowCol();
wordStorage(wordBoard);
}
public static void main(String[] args) {
Grid grid = new Grid();
grid.playWordSearch();
}
/**
* This method asks the user to input an integer for Rows/Columns
* And gracefully handles invalid input :)
*
* @return
*/
public char[][] numRowCol() {
int array[] = new int[100];
boolean again = true;
while (again) {
for (int i = 0; i <= 1; i++) {
try {
if (i == 0) {
System.out.printf("Enter a number of rows (2-15): %n");
array[i] = keyboard.nextInt();
if (array[i] > 15 || array[i] < 2) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
}
}
} catch (InputMismatchException e) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
keyboard.next();
e.printStackTrace();
}
try {
if (i == 1) {
System.out.printf("Enter a number of columns (2-15): %n");
array[i] = keyboard.nextInt();
if (array[i] > 15 || array[i] < 2) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
}
}
} catch (InputMismatchException e) {
System.out.printf("Requirements: int between [2-15] %n");
i -= 1;
keyboard.next();
e.printStackTrace();
}
}
again = false;
}
System.out.printf("The amount of rows you have entered: %d%nAnd the amount of columns: %d%n", array[0], array[1]);
char[][] arrayArray = new char[array[0]][array[1]];
return arrayArray;
}
public int wordStorage(char[][] arrayArray) {
int size;
String word[] = new String[arrayArray.length];
int sizeRow = arrayArray.length;
int sizeCol = arrayArray[1].length;
System.out.printf("rows: %d%n", sizeRow);
System.out.printf("cols: %d%n", sizeCol);
for (int i = 0; i < arrayArray.length; i++) {
System.out.printf("Enter a word with less than %d characters: %n", sizeCol);
word[i] = keyboard.next();
}
keyboard.close();
for (int i = 0; i < word.length; ++i) {
arrayArray[i] = word[i].toCharArray();
}
System.out.println(Arrays.deepToString(arrayArray).replace("], ",
"]\n").replace("[[", "[").replace("]]", "]"));
return 100;
}
}
首先,您的网格是 2d int数组而不是 2d char数组(您的函数还返回 2d int 数组而不是 2d char 数组)。其次,我只是添加了一个简单的 for 循环来将用户输入的单词分配到数组中。
如果您需要填充列中的空白,只需生成随机 char 值,填充 char 数组以满足大小值,然后像我上面那样将它们分配到数组中。
推荐阅读
- reactjs - 无法读取“未定义”的属性:从 React 到 Express 的 POST 请求
- java - 如何将 DataSetIterator 拆分为测试和训练迭代器?
- python - 从字典列表中提取数据python 3
- c - 是否可以使用 Ruby FFI 向函数发送 Ruby (C API) 值?
- python - 存储桶名称必须以数字或字母开头和结尾
- windows - 汇编中两个数字的加法
- javascript - 获取、自定义和重新编译 Chrome/MS-Edge 中存在的 PDFium 扩展的源代码
- c# - .AddOpenIdConnect() 中间件说明
- reactjs - setState 更改后输入值未更新...递增和递减
- python - 如何使用python将字符串粘贴到浏览器中的textarea?