gpt4 book ai didi

java - WordSearch 类的 boolean 问题

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:55:56 24 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

关于java - WordSearch 类的 boolean 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22638381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com