WordSearch类的布尔故障

时间:2014-03-25 15:01:14

标签: java arrays boolean

如果找到单词,我很难将我的布尔值更改为true。

import java.util.Scanner; 
public class WordSearch
{
    private char[][] array;
    private boolean found;

    public WordSearch(char[][] inArray)
    {
        array = inArray;
    }

    public void play()
    {
        Scanner in = new Scanner(System.in);  

        String choice = "";

        while(!choice.equals("end"))
        {

            print();
            System.out.println("What word do you want to search for? (Type end to quit)");
            choice = in.nextLine();
            System.out.println();

            switch (choice)
            {
                case "end":
                break;
                default:
                search(choice);
                break;
            }
        }

    }

    public void print()
    {
        for(int i = 0; i < array.length; i++)
        {
            for(int j = 0; j < array[0].length; j++)
            {
                System.out.print(array[i][j]);
            }
            System.out.println();
        }
        System.out.println();
    }

    public void search(String inWord)
    {
        // Puts inWord into an array
        char[] word = new char[inWord.length()];
        for(int i = 0; i < inWord.length(); i++)
        {
            word[i] = inWord.charAt(i);
        }

        for(int i = 0; i < array.length; i++)// goes to each row
        {
            for(int j = 0; j < array[0].length; j++)// goes through every letter
            {
                if(array[i][j] == word[0])// asks if it matches
                {
                    lookHorizantal(word, array, i, j, found);
                    lookVertical(word, array, i, j, found);
                    lookDiagnal(word, array, i, j, found);
                }
            }
        }

        if(!found)
        {
            System.out.println(inWord + "was not found!");
        }
        System.out.println();
    }

    public void lookHorizantal(char[] inWord, char[][] inArray, int row, int column, boolean ifFound)
    {
        int counter = 1; //set this to one because we already found the first letter
        column += 1;
        for(int i = 0; i < inWord.length; i++)
        {
            if(column < 15 && counter < inWord.length)
            {
                if(inArray[row][column] == inWord[counter])
                {
                    //System.out.println("WE FOUND A LETTER AT COLUMN: " + row + " Row: " + column);
                    counter += 1;
                    column += 1;
                }
            }
        }

        if(counter == inWord.length)
        {
            ifFound = true;
            System.out.println(printChar(inWord) + " found horizontally at row " + row + " and column " + (column - counter) + "!");
        }
    }

    public void lookVertical(char[] inWord, char[][] inArray, int row, int column, boolean ifFound)
    {
        int counter = 1; //set this to one because we already found the first letter
        row += 1;
        for(int i = 0; i < inWord.length; i++)
        {
            if(row < 10 && counter < inWord.length)
            {
                if(inArray[row][column] == inWord[counter])
                {
                    //System.out.println("WE FOUND A LETTER AT COLUMN: " + row + " Row: " + column);
                    counter += 1;
                    row += 1;
                }
            }
        }

        if(counter == inWord.length)
        {
            ifFound = true;
            System.out.println(printChar(inWord) + " found vertically at row " + (row - counter) + " and column " + column + "!");
        }
    }

    public void lookDiagnal(char[] inWord, char[][] inArray, int row, int column, boolean ifFound)
    {
        int counter = 1; //set this to one because we already found the first letter
        row += 1;
        column += 1;
        for(int i = 0; i < inWord.length; i++)
        {
            if(row < 10 && column < 15 && counter < inWord.length)
            {
                if(inArray[row][column] == inWord[counter])
                {
                    //System.out.println("WE FOUND A LETTER AT COLUMN: " + row + " Row: " + column);
                    counter += 1;
                    row += 1;
                    column +=1;
                }
            }
        }

        if(counter == inWord.length)
        {
            ifFound = true;
            System.out.println(printChar(inWord) + " found diagnolly at row " + (row - counter) + " and column " + (column - counter) + "!");
        }
    }



    public String printChar(char[] inChar)
    {
        String complete = "";
        for(int i = 0; i < inChar.length; i++)
        {
            complete += inChar[i];
        }
        return complete;
    }
}

每当我找到一个单词时,它返回false并运行未找到的单词。我没有声明布尔对吗?还是在它退出方法时重置?

如果你想测试它,请看驱动程序:

import java.util.*;
import java.io.*;

public class Driver
{    
    public static void main (String[] args)
    {
        // try block needed to read in file
        try
        {
            //open the file "sampleSearch.txt"
            FileReader fileName = new FileReader("sampleSearch.txt");
            Scanner fileRead = new Scanner(fileName);

            //read in the number of rows
            int numRow = fileRead.nextInt();
            fileRead.nextLine();

            //read in the number of columns
            int numCol = fileRead.nextInt();
            fileRead.nextLine();

            //create a 2d array to hold the characters in the file
            char[][] array = new char[numRow][numCol];

            //for each row in the file
            for (int r = 0; r < numRow; r++)
            {
                //read in the row as a string
                String row = fileRead.nextLine();

                //parse the string into a sequence of characters
                for (int c = 0; c < numCol; c++)
                {
                    //store each character in the 2d array
                    array[r][c] = row.charAt(c);
                }
            }

            //create a new instance of the WordSearch class
            WordSearch ws = new WordSearch(array);

            //play the game
            ws.play();
        }
        catch (FileNotFoundException exception)
        {
            //error is thrown if file cannot be found.  See directions or email me...
            System.out.println("File Not Found");
        }

    }    
}

和heres sampleSearch.txt:

10
15
fqexfecmxdvjlgu
cxomfslieyitqtz
nucatfakuxofegk
hfytpnsdlhcorey
pgrhdqsypyscped
ckadhyudtioapje
yerjodxnqzztfmf
hypmmgoronkzhuo
hdskymmpkzokaao
amuewqvtmrlglad

2 个答案:

答案 0 :(得分:6)

您无法在被调用的方法中修改不可变原语布尔值(甚至是布尔包装器),对方法的更改不会保留在方法之外。相反,你有两个选择 -

  1. 将状态存储在对象字段中,并为该字段提供访问者(例如,获取者),
  2. 从该方法返回布尔值(或布尔值)(而不是“void”)。

答案 1 :(得分:3)

当你传递found作为参数时,你传递的是它的值,而不是对它的引用,所以基本上你要创建一个方法实例变量。

我建议两个选项

第一个是返回值:

found = lookHorizantal(word, array, i, j);
// Maybe check between these search calls if it is found?
found = lookVertical(word, array, i, j);
// Maybe check between these search calls
found = lookDiagnal(word, array, i, j);
// Maybe check between these search calls if it is found?

public boolean lookHorizantal(char[] inWord, char[][] inArray, int row, int column)
{
    // OMITTED CODE
    if(counter == inWord.length)
    {
        return true; /*******************/
        // OMITTED CODE
    }
}

或只引用类变量:

public void lookHorizantal(char[] inWord, char[][] inArray, int row, int column)
{
    // OMITTED CODE
    if(counter == inWord.length)
    {
        found = true; /*******************/
        // OMITTED CODE
    }
}

这些开关将直接更改类变量found