0

在过去的几天里,我一直在为大学做一个 Java 项目。我遇到了一些问题,在本网站用户的帮助下我克服了这些问题。我正在做的是单词搜索,它将用户输入的单词作为字符串的数组列表,并将它们随机映射到二维数组上。我已经创建了用于选择行和列、选择单词的方向以及检查是否有足够的空白空间用于所述单词的方法。我在编译该代码时遇到了一些错误。这是我之前的问题,所以你可以看到,从那时起我已经取得了一些进展:) https://stackoverflow.com/questions/10193291/adding-words-to-a-2-d-array

具体来说,问题是我试图通过使用随机数来决定选择哪种方法来引用 doitfit 方法(上、下、左或右)。它没有编译:/

    import java.io.* ;

import java.util.ArrayList ;

public class WordSearchPuzzle

{

    private char[][] puzzle ;

    private ArrayList<String> puzzleWords ;

    private int letterCount = 0 ;

    private int gridDimensions;

    public static int row, column;




    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)

    {

        this.puzzleWords = userSpecifiedWords ;




    }




    private void createPuzzleGrid()

    {

        int i, itemLength;

        String item;

        for (i = 0; i < puzzleWords.size(); i++) {

            item = puzzleWords.get(i);

            itemLength = item.length();

            letterCount = letterCount + itemLength;

        }

        gridDimensions = letterCount * 2;

        puzzle = new char[gridDimensions][gridDimensions] ;

    }




    private void generateWordSearchPuzzle()

    {




    }




    public void northSouthEastWest(String word)

    {

        int upDownLeftRight, north, south, east, west;

        north = 1;

        south = 2;

        east = 3;

        west = 4;

        String Word;

        upDownLeftRight = (int)(Math.random() * 4);

        if(upDownLeftRight == north){

            fitWordNorth(word);

        }else if(upDownLeftRight == south){

            fitWordSouth(word);

        }else if(upDownLeftRight == east){

            fitWordEast(word);

        }else if(upDownLeftRight == west){

            fitWordWest(word);

        }

    }




    public void firstSpace(String word) 

    {  

        row = (int)(Math.random() * gridDimensions);

        column = (int)(Math.random() * gridDimensions);




        if(puzzle[row][column] != ' ') { 

            firstSpace(word);

        } else {

            northSouthEastWest(word);

        }

    }




    public void fitWordNorth(String word)

    {

        boolean clear = false;

        int p, i; 

        if(row >= word.length()){

            for(i = row - 1; i < word.length(); i--){

                if(puzzle[i][column] != ' '){

                    firstSpace(word);

                }else{

                    clear = true;

                }




                if(clear == true){

                    for(p = 0; p < word.length(); p++){

                        puzzle[row - p][column] = word.charAt(p);

                    }

                }else{

                    firstSpace(word);

                }

            }

        }

    }




    public void fitWordSouth(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(row >= word.length()){

            for(i = row + 1; i < word.length(); i++){

                if(puzzle[i][column] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row + p][column] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }




    public void fitWordWest(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(column >= word.length()){

            for(i = column - 1; i < word.length(); i--){

                if(puzzle[row][i] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row][column - p] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }




    public void fitWordEast(String word)

    {

        boolean clear = false;

        int row, column, p, i; 

        if(column >= word.length()){

            for(i = column + 1; i < word.length(); i++){

                if(puzzle[row][i] != ' '){

                    firstSpace(word);

                }

                clear = true;

            }

        }else{

            firstSpace(word);

        }

        if(clear == true){

            for(p = 0; p < word.length(); p++){

                puzzle[row][column + p] = word.charAt(p);

            }

        }else{

            firstSpace(word);

        }

    }

}
4

2 回答 2

1

您正在从您的 northSouthEastWest() 函数调用 fitWordNorth(Word)、fitWordSouth(Word)、fitWordEast(Word)、fitWordWest(Word) 函数。编译器抱怨,因为在类范围或方法范围内没有定义为 Word 的变量。

我也没有看到程序中的任何地方都调用了 northSouthEastWest() 函数。请将该函数定义为 northSouthEastWest(String Word) 以使该函数起作用。

于 2012-04-18T12:48:21.470 回答
0

在进一步查看您的代码后,可能会有很多建议。当我有时间时,我会更新这个答案。您可能有一个主要方法..类似

/**
     * @param args
     */
    public static void main(String[] args) {
        if(null!=args && args.length>0){
            WordSearchPuzzle wsp = new WordSearchPuzzle(Arrays.asList(args));
        }else{
            System.err.println("Please enter words to search");
        }
        //TODO
        //Call your functions in the series that you want...
    }

你的构造函数可以像

public WordSearchPuzzle(List<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;
    }

将您的 puzzleWords 变量定义为 List 而不是 ArrayList

 private List<String> puzzleWords ;

您的 northSouthEastWest 函数可以是:(north, south, east, and west 可以是常量)

    public static final int NORTH= 0;
    public static final int SOUTH = 1;
    public static final int EAST = 2;
    public static final int WEST = 3;

    public void northSouthEastWest(String Word)
    {
        int upDownLeftRight = (int)(Math.random() * 3);
        switch(upDownLeftRight){
        case NORTH: fitWordNorth(Word);break;
        case SOUTH: fitWordSouth(Word);break;
        case WEST: fitWordWest(Word);break;
        case EAST:  fitWordEast(Word);break;
        }
    }

您可以考虑创建另一个具有属性 row、column、p、i 的类,并将该对象的实例包含在您的拼图类中。

于 2012-04-18T14:26:45.990 回答