链接方法/小修正 - Java

时间:2012-04-18 12:23:40

标签: java arraylist multidimensional-array

过去几天我一直在为大学做一个Java项目。我已经打了几个bumnps,我在这个网站的用户帮助下克服了这个问题。我正在做的是一个单词搜索,它将用户输入的单词作为字符串的数组列表,并在2维数组上随机映射。我已经创建了选择行和列的方法,用于选择单词的方向,以及检查是否有足够明确的空间用于所述单词。我在编译代码时遇到了一些错误。这是我之前的问题,所以你可以看到,我从那时起取得了一些进展:) 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);

        }

    }

}

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分: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函数可以是:(北,南,东,西可以是常量)

    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创建另一个类,并在Puzzle类中包含此对象的实例。