0

ここ数日、大学向けの Java プロジェクトに取り組んでいます。いくつかの障害にぶつかりましたが、このサイトのユーザーの助けを借りて克服しました。私がやっているのは、ユーザーが入力した単語を文字列の配列リストとして受け取り、それらを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);

        }

    }

}
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 変数を ArrayList ではなく List として定義します

 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を持つ別のクラスを作成し、このオブジェクトのインスタンスをパズルクラスに含めることを検討してください。

于 2012-04-18T14:26:45.990 に答える