【问题标题】:Boolean troubles with WordSearch classWordSearch 类的布尔问题
【发布时间】:2014-05-03 12:29:11
【问题描述】:

如果找到该单词,我很难将布尔值更改为 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 并运行 not found。我不是在声明布尔值吗?还是在退出方法时会重置?

如果你想测试一下,这里是驱动程序:

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:

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

【问题讨论】:

    标签: java arrays boolean


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      当您将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

      【讨论】:

        猜你喜欢
        • 2011-10-04
        • 1970-01-01
        • 2016-10-26
        • 1970-01-01
        • 2012-11-27
        • 1970-01-01
        • 1970-01-01
        • 2019-09-14
        相关资源
        最近更新 更多