我正在嘗試使用用戶輸入從文本文件中搜索二維數組,但到目前爲止,我要麼終於得到該單詞不在二維數組中(它是)還是它結束說什麼用戶輸入是在陣列中。例如,紅色字在數組中,但它輸出多次而不是一次,或者輸出該單詞在數組中多次。我輸入的單詞文件位於前兩個數字用於2D數組大小的位置。任何人都可以給我一些暗示我做錯了什麼,因爲我需要一個新的眼睛,因爲我卡住了。二維數組字搜索
6 6
d e v o l g
r e d p h k
q c h z j c
p o a a f o
v a m m n l
q t f o x b
我的代碼如下,如果我需要清理一下代碼,只是讓我知道,因爲我敢肯定,有些事情可能有點難以理解,因爲我一直工作在此的某個時候。
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
public class WordSearch {
public static void main(String[] args) {
char[][] puzzle = null;
puzzle = fill(puzzle);
// create scanner for user input to take the word for searching
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
System.out.print("Enter the word you wish to search for (limit of four characters): ");
String wordToFind = in.nextLine();
play(wordToFind, puzzle);
printPuzzle(puzzle);
}
public static char[][] fill(char[][] puzzle) {
// file Reading
boolean flag = true;// boolean flag is used for prompting user if the file location is incorrect
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
while (flag) {
System.out.print("Please input the text file locaiton: ");
String fileLoc = in.nextLine();
try {
File f = new File(fileLoc);
@SuppressWarnings("resource")
Scanner fileRead = new Scanner(f);
int row = fileRead.nextInt();// i
int col = fileRead.nextInt();// j
puzzle = new char[row][col];
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[0].length; j++) {
puzzle[i][j] = fileRead.next().charAt(0);
}
}
flag = false;// breaks the loop so the user isn't re-prompt for
// file location
} catch (FileNotFoundException e) {
}
}
return puzzle;
}
public static void printPuzzle(char[][] puzzle) {
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[0].length; j++) {
System.out.print(puzzle[i][j] + " ");
}
System.out.println();
}
}
public static void play(String word, char[][] puzzle) {
for (int i = 0; i < puzzle.length; i++) {
for (int j = 0; j < puzzle[0].length; j++) {
if (checkUp(puzzle, word, i, j) == true)
{
System.out.println("The word " + word + " was found by the method checkUp beginnning in cell ");
}
if (checkDown(puzzle, word, i, j) == true)
{
System.out.println("The word " + word + " was found by the method checkDown beginning in cell");
}
if(checkRight(puzzle, word, i, j) == true)
{
System.out.println("The word " + word + " was found by the method checkDown beginning in cell");
}
if(checkLeft(puzzle, word, i, j) == true)
{
System.out.println("The word " + word + " was found by the method checkLeft beginning in cell");
}
if(checkUp(puzzle, word, i, j) != true && checkDown(puzzle, word, i, j) != true && checkRight(puzzle, word, i, j) != true && checkLeft(puzzle, word, i, j) != true)
{
System.out.println("The word " + word + " was not in the puzzle");
break;
}
}
}
}
/**
* searches for the user defined word going up
* @param puzzle
* @param word
* @param i
* @param j
* @return
*/
public static boolean checkUp(char[][] puzzle, String word, int i, int j) {
char search = word.charAt(0);
for (i = 0; i < puzzle.length; i++) {
for (j = 0; j < puzzle[0].length; j++) {
if (search != puzzle[i][j]) {
return false;
}
else {
i = i - 1;
if (i < 0) {
return false;
}
}
}
}
return true;
}
/**
* searches for the user defined word going down
* @param puzzle
* @param word
* @param i
* @param j
* @return
*/
public static boolean checkDown(char[][] puzzle, String word, int i, int j) {
char search = word.charAt(0);
for (i = 0; i < puzzle.length; i++) {
for (j = 0; j < puzzle[0].length; j++) {
if (search != puzzle[i][j]) {
return false;
}
else {
i = i + 1;
if (i < 0) {
return false;
}
}
}
}
return true;
}
/**
* searches for the user defined word going left
* @param puzzle
* @param word
* @param i
* @param j
* @return
*/
public static boolean checkLeft(char[][] puzzle, String word, int i, int j) {
char search = word.charAt(0);
for (i = 0; i < puzzle.length; i++) {
for (j = 0; j < puzzle[0].length; j++) {
if (search != puzzle[i][j]) {
return false;
}
else {
j = j - 1;
if (j < 0) {
return false;
}
}
}
}
return true;
}
/**
* this method will return true if the user defined word is found going right
* @param puzzle
* @param word
* @param i
* @param j
* @return
*/
public static boolean checkRight(char[][] puzzle, String word, int i, int j) {
char search = word.charAt(0);
for (i = 0; i < puzzle.length; i++) {
for (j = 0; j < puzzle[0].length; j++) {
if (search != puzzle[i][j]) {
return false;
}
else {
j = j + 1;
if (j < 0) {
return false;
}
}
}
}
return true;
}
}
而不是壓制的警告,你應該使用[嘗試 - 與資源(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html)。 – 4castle
好吧,我會試試 –