首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带WordSearch类的布尔问题

带WordSearch类的布尔问题
EN

Stack Overflow用户
提问于 2014-03-25 15:01:14
回答 2查看 143关注 0票数 3

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

代码语言:javascript
复制
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并运行not。我不是在声明布尔值吗?或者,当它脱离方法时,它是否被重置了?

这是驱动程序,如果您想测试它的话:

代码语言:javascript
复制
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");
        }

    }    
}

下面是sampleSearch.txt:

代码语言:javascript
复制
10
15
fqexfecmxdvjlgu
cxomfslieyitqtz
nucatfakuxofegk
hfytpnsdlhcorey
pgrhdqsypyscped
ckadhyudtioapje
yerjodxnqzztfmf
hypmmgoronkzhuo
hdskymmpkzokaao
amuewqvtmrlglad
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-25 15:07:34

当您将found作为参数传递时,您传递的是它的值,而不是对它的引用,因此实际上您正在创建一个方法实例变量。

我建议两种选择

第一种方法是返回值:

代码语言:javascript
复制
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
    }
}

或者仅仅引用类变量:

代码语言:javascript
复制
public void lookHorizantal(char[] inWord, char[][] inArray, int row, int column)
{
    // OMITTED CODE
    if(counter == inWord.length)
    {
        found = true; /*******************/
        // OMITTED CODE
    }
}

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

票数 3
EN

Stack Overflow用户

发布于 2014-03-25 15:05:01

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

  1. 将状态存储在对象字段中,并为该字段(例如getter)提供访问器,
  2. 从该方法返回布尔值(或布尔值)(而不是void)。
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22638381

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档