Java 填字游戏二维数组
Java Crossword 2d Array
package assignment2;
import java.util.Random;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
int wordAmount = wordAmount();
String[] words = wordArray(wordAmount);
displayWords(words, wordAmount);
char[][] puzzleGrid = generatePuzzleGrid(words, wordAmount);
//System.out.println(Arrays.deepToString(puzzleGrid)); //just using this to test
printPuzzleGrid(puzzleGrid, wordAmount);
}
public static int wordAmount(){
Scanner input = new Scanner(System.in);
System.out.print("How many words would you like in the word search(5-15): ");
int wordAmount = input.nextInt();
while(wordAmount < 5 || wordAmount > 20){
System.out.println("The number you've requested is either to large or to small.");
System.out.print("How many words would you like in the word search(5-20): ");
wordAmount = input.nextInt();
}
return wordAmount;
}
public static String[] wordArray(int wordAmount){
String[] words = new String[wordAmount];
Scanner input = new Scanner(System.in);
for(int i = 0; i < wordAmount; i++){
System.out.print("Enter a word: ");
words[i] = (input.nextLine().toUpperCase());
for(int j = 0; j < wordAmount; j++){
if(j == i) break;
while(words[i].contains(words[j])){
System.out.print("The word you entered has already been entered, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
while(words[i].length() > 10 || words[i].length() <= 2 || words[i].contains(" ")){
System.out.print("The word you entered has been rejected, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
return words;
}
public static void displayWords(String[] words, int wordAmount){
System.out.print("The words you must find are: ");
for(int w = 0; w < wordAmount; w++){
System.out.print(words[w] + " ");
}
System.out.println("");
}
public static char[][] generatePuzzleGrid(String[] words, int wordAmount){
char[][] puzzleGrid = new char[wordAmount][wordAmount];
Random rand = new Random();
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < words[across].length(); down++){
puzzleGrid[across][down] = words[across].charAt(down);
for(int filler = wordAmount; filler >= words[across].length(); filler--){
puzzleGrid[across][filler] = (char)(rand.nextInt(26) + 'A'); //this is the line with the problem
}
}
}
return puzzleGrid;
}
public static void printPuzzleGrid(char[][] puzzleGrid, int wordAmount){
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < wordAmount; down++){
System.out.print(" " + puzzleGrid[down][across]);
}
System.out.println("");
}
}
}
看来我的上一个问题已经解决了,但现在我遇到了一个新问题。
错误:线程异常 "main" java.lang.ArrayIndexOutOfBoundsException:5
在作业 2.test2.generatePuzzleGrid(test2.java:63)
在作业 2.test2.main(test2.java:10)
C:\Users\Higle\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java 返回:1
构建失败(总时间:9 秒)
您似乎是在对少于 5 个字符的字符串调用 charAt(5)。你可以这样做
if(words[across].length() < (down-1)){
continue;
}
确保你不会得到那个特定的错误......另外,你可能想知道 charAt() returns char 的索引从 0->length()-1
它接缝应用程序运行没有任何问题,唯一关心的是如果用户提供的 wordAmount(网格中的字符数)大于他提供的单词会发生什么。所以最好在 wordAmount 上添加验证,如下所示。
public static char[][] generatePuzzleGrid(String[] words, int wordAmount) {
if(wordAmount>words.length){
wordAmount = words.length;
}
char[][] puzzleGrid = new char[wordAmount][wordAmount];
for (int across = 0; across < wordAmount; across++) {
for (int down = 0; down < words[across].length(); down++) {
puzzleGrid[across][down] = words[across].charAt(down);/////////////////
}
}
return puzzleGrid;
}
package assignment2;
import java.util.Random;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
int wordAmount = wordAmount();
String[] words = wordArray(wordAmount);
displayWords(words, wordAmount);
char[][] puzzleGrid = generatePuzzleGrid(words, wordAmount);
//System.out.println(Arrays.deepToString(puzzleGrid)); //just using this to test
printPuzzleGrid(puzzleGrid, wordAmount);
}
public static int wordAmount(){
Scanner input = new Scanner(System.in);
System.out.print("How many words would you like in the word search(5-15): ");
int wordAmount = input.nextInt();
while(wordAmount < 5 || wordAmount > 20){
System.out.println("The number you've requested is either to large or to small.");
System.out.print("How many words would you like in the word search(5-20): ");
wordAmount = input.nextInt();
}
return wordAmount;
}
public static String[] wordArray(int wordAmount){
String[] words = new String[wordAmount];
Scanner input = new Scanner(System.in);
for(int i = 0; i < wordAmount; i++){
System.out.print("Enter a word: ");
words[i] = (input.nextLine().toUpperCase());
for(int j = 0; j < wordAmount; j++){
if(j == i) break;
while(words[i].contains(words[j])){
System.out.print("The word you entered has already been entered, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
while(words[i].length() > 10 || words[i].length() <= 2 || words[i].contains(" ")){
System.out.print("The word you entered has been rejected, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
return words;
}
public static void displayWords(String[] words, int wordAmount){
System.out.print("The words you must find are: ");
for(int w = 0; w < wordAmount; w++){
System.out.print(words[w] + " ");
}
System.out.println("");
}
public static char[][] generatePuzzleGrid(String[] words, int wordAmount){
char[][] puzzleGrid = new char[wordAmount][wordAmount];
Random rand = new Random();
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < words[across].length(); down++){
puzzleGrid[across][down] = words[across].charAt(down);
for(int filler = wordAmount; filler >= words[across].length(); filler--){
puzzleGrid[across][filler] = (char)(rand.nextInt(26) + 'A'); //this is the line with the problem
}
}
}
return puzzleGrid;
}
public static void printPuzzleGrid(char[][] puzzleGrid, int wordAmount){
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < wordAmount; down++){
System.out.print(" " + puzzleGrid[down][across]);
}
System.out.println("");
}
}
}
看来我的上一个问题已经解决了,但现在我遇到了一个新问题。
错误:线程异常 "main" java.lang.ArrayIndexOutOfBoundsException:5 在作业 2.test2.generatePuzzleGrid(test2.java:63) 在作业 2.test2.main(test2.java:10) C:\Users\Higle\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java 返回:1 构建失败(总时间:9 秒)
您似乎是在对少于 5 个字符的字符串调用 charAt(5)。你可以这样做
if(words[across].length() < (down-1)){
continue;
}
确保你不会得到那个特定的错误......另外,你可能想知道 charAt() returns char 的索引从 0->length()-1
它接缝应用程序运行没有任何问题,唯一关心的是如果用户提供的 wordAmount(网格中的字符数)大于他提供的单词会发生什么。所以最好在 wordAmount 上添加验证,如下所示。
public static char[][] generatePuzzleGrid(String[] words, int wordAmount) {
if(wordAmount>words.length){
wordAmount = words.length;
}
char[][] puzzleGrid = new char[wordAmount][wordAmount];
for (int across = 0; across < wordAmount; across++) {
for (int down = 0; down < words[across].length(); down++) {
puzzleGrid[across][down] = words[across].charAt(down);/////////////////
}
}
return puzzleGrid;
}