Java 如何将任何内容显示到根据用户输入而更改的数组中?
基本上,我必须为我的类创建一个随机单词搜索生成器,我可以让用户输入单词搜索将有多少行和多少列,我可以让我的程序要求用户输入x个要查找的单词数量(基于行)和单词长度(基于列)然而,就我的一生而言,我无法找到如何在我制作的这个神秘的网格中打印任何东西。我只需要了解如何在数组/网格/板中添加随机字母。谢谢Java 如何将任何内容显示到根据用户输入而更改的数组中?,java,Java,基本上,我必须为我的类创建一个随机单词搜索生成器,我可以让用户输入单词搜索将有多少行和多少列,我可以让我的程序要求用户输入x个要查找的单词数量(基于行)和单词长度(基于列)然而,就我的一生而言,我无法找到如何在我制作的这个神秘的网格中打印任何东西。我只需要了解如何在数组/网格/板中添加随机字母。谢谢 import java.util.*; public class Assignment2 { Scanner keyboard = new Scanner(System.in);
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.*;
公共课堂作业2{
扫描仪键盘=新扫描仪(System.in);
专用int[][]字板;
私有char[][]游戏板;
私有字符串[]字列表;
私有静态int行;
私有静态int col;
随机rnd=新随机();
public void playWordSearch(){
wordBoard=numRowCol();
字存储(字板);
}
/**
*此方法要求用户为行/列输入一个整数
*并优雅地处理无效输入:)
*
*@返回
*/
公共int[]numRowCol(){
int数组[]=新的int[100];
布尔值=真;
同时(再次){
for(int i=0;i 15 | |数组[i]<2){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
}
}
}捕获(输入不匹配异常e){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
键盘。下一步();
e、 printStackTrace();
}
试一试{
如果(i==1){
System.out.printf(“输入若干列(2-15):%n”);
数组[i]=keyboard.nextInt();
if(数组[i]>15 | |数组[i]<2){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
}
}
}捕获(输入不匹配异常e){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
键盘。下一步();
e、 printStackTrace();
}
}
再次=假;
}
System.out.printf(“您输入的行数:%d%n,列数:%d%n”,数组[0],数组[1]);
int[]arrayArray=new int[array[0]][array[1]];
返回阵列射线;
}
公共int字存储(int[][]arrayArray){
整数大小;
字符串字[]=新字符串[arrayArray.length];
int sizeRow=arrayArray.length;
int-sizeCol=arrayArray[1]。长度;
System.out.printf(“行:%d%n”,sizeRow);
System.out.printf(“列:%d%n”,sizeCol);
for(int i=0;i
导入java.util.*;
公共课堂作业2{
扫描仪键盘=新扫描仪(System.in);
专用字符[][]字板;
私有char[][]游戏板;
私有字符串[]字列表;
私有静态int行;
私有静态int col;
随机rnd=新随机();
public void playWordSearch(){
wordBoard=numRowCol();
字存储(字板);
}
公共静态void main(字符串[]args){
网格=新网格();
playWordSearch();
}
/**
*此方法要求用户为行/列输入一个整数
*并优雅地处理无效输入:)
*
*@返回
*/
公共字符[][]numRowCol(){
int数组[]=新的int[100];
布尔值=真;
同时(再次){
for(int i=0;i 15 | |数组[i]<2){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
}
}
}捕获(输入不匹配异常e){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
键盘。下一步();
e、 printStackTrace();
}
试一试{
如果(i==1){
System.out.printf(“输入若干列(2-15):%n”);
数组[i]=keyboard.nextInt();
if(数组[i]>15 | |数组[i]<2){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
}
}
}捕获(输入不匹配异常e){
系统输出打印F(“要求:整数在[2-15]%n]之间”);
i-=1;
键盘。下一步();
e、 printStackTrace();
}
}
再次=假;
}
System.out.printf(“您输入的行数:%d%n,列数:%d%n”,数组[0],数组[1]);
char[][]arrayArray=new char[array[0]][array[1]];
返回阵列射线;
}
公共int字存储(char[][]arrayArray){
整数大小;
字符串字[]=新字符串[arrayArray.length];
int sizeRow=arrayArray.length;
int-sizeCol=arrayArray[1]。长度;
System.out.printf(“行:%d%n”,sizeRow);
System.out.printf(“列:%d%n”,sizeCol);
for(int i=0;iimport 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;
}
}