Java填字游戏2d阵列

时间:2016-12-04 00:10:55

标签: java runtimeexception

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("");
    }
}    
}

看来我的最后一个问题已经解决了,但现在我遇到了一个新问题。

错误:线程&#34; main&#34;中的异常java.lang.ArrayIndexOutOfBoundsException:5                 在assignment2.test2.generatePuzzleGrid(test2.java:63)                 在assignment2.test2.main(test2.java:10)             C:\ Users \ Higle \ AppData \ Local \ NetBeans \ Cache \ 8.2 \ executor-snippets \ run.xml:53:Java返回:1             建筑失败(总时间:9秒)

2 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

看起来你在少于5个字符的字符串上调用charAt(5)。你可以做点什么

if(words[across].length() < (down-1)){ 
    continue;
}

确保你没有得到那个特定错误...另外,你可能想知道charAt()从0-&gt; length() - 1

返回一个char的索引