gpt4 book ai didi

java - 循环并不能处理所有事情

转载 作者:行者123 更新时间:2023-11-29 07:54:05 25 4
gpt4 key购买 nike

所以我正在编写一个填字游戏解算器,除了 1 个故障外,我几乎 98% 的代码都能正常工作。我有一个循环,它从文件中读取 1 个单词,然后将该字符串与一个字符数组进行比较,以查找该字符串在数组中的位置。到目前为止,循环读取了所有单词并找到了它们的位置,但我的问题是它跳过了我列表中的 3 个单词。我知道这些词在 char 数组中,但由于某种原因它们没有被拾取。有任何想法吗?

这是我的代码:

    import java.io.File;
import java.util.Scanner;


public class Test {

public static void main(String[] args) throws Exception{
File file = new File("puzzle.txt");
Scanner sc = new Scanner(file);
int row = sc.nextInt();
int col = sc.nextInt();
sc.nextLine();

//Make an array to hold the puzzle
char[][] puzzle = new char[row][col];


//Read in the strings from the file into the array.
for (int i=0; i<row; i++){
String getChar = (new String(sc.next()));
for(int j = 0;j<col; j++){
puzzle[i][j] = getChar.charAt(j);
}
}

//Read the number of words and move to the next line
int numwords = sc.nextInt();
sc.nextLine();


//look for each word
for(int i=0; i<numwords; i++){
String word = new String();
word = sc.nextLine();
System.out.printf("This is word: %s\n", word);

//arrays to hold the direction.
int [] movx ={-1, -1, -1, 0, 0, 1, 1, 1};
int [] movy ={-1, 0, 1, -1, 1, -1, 0, 1};

//this variable will hold if we found or not the string in the puzzle
boolean found = false;

//find the words in the puzzle
for(int m = 0; m < puzzle.length; m++) {
for(int n = 0; n < puzzle[0].length; n++) {
if(puzzle[m][n] == word.charAt(0)){
for (int o = 0; o < 8; o++){
if(check(m, n, word, puzzle, movx[o], movy[o])){
System.out.printf("%s found at position (%d, %d)\n\n", word, m, n);
found = true;
break;
}
}

}
}
}
if (!found){
System.out.printf("%s was not found\n\n", word);
}
}

//Close the scanner
sc.close();
}
//This is your generic-direction function
public static boolean check(int row, int col, String word, char[][] puzzle, int offsetx, int offsety){

//start with the current position
int x = row;
int y = col;

for (int i = 0; i < word.length(); i++){
char c = word.charAt(i);

//Is not equal
if (puzzle[x][y] != c) return false;

x += offsetx;
y += offsety;

//check the boundaries, if we go out then we didn't find the word;
if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;
}

return true;
}

}

(编辑区域)

根据我对 print 语句的诊断,当 i 为 3、8 和 9 时,单词搜索找不到单词。我相信这是在这个循环之后:

    //find the words in the puzzle
for(int m = 0; m < puzzle.length; m++)

因为在这个循环之前,我之前的循环遍历了每个单词。 word 被传递到这个循环内的循环中。所以它一直通过我对数组中每个点的所有检查,然后即使它在拼图中也只是作为 false 通过。

(/编辑区域)

在这个循环之前,我测试了

我正在阅读的文件:

    10 10
WVERTICALL
ROOAFFLSAB
ACRILIATOA
NDODKONWDC
DRKESOODDK
OEEPZEGLIW
MSIIHOAERA
ALRKRRIRER
KODIDEDRCD
HELWSLEUTH
10
WEEK
FIND
RANDOM
SLEUTH
BACKWARD
VERTICAL
DIAGONAL
WIKIPEDIA
HORIZONTAL
WORDSEARCH

这是它打印的内容(WEEK 不在拼图中):

    This is word: WEEK and this is i 0
WEEK was not found

This is word: FIND and this is i 1
FIND found at position (1, 4)

This is word: RANDOM and this is i 2
RANDOM found at position (1, 0)

This is word: SLEUTH and this is i 3
SLEUTH was not found

This is word: BACKWARD and this is i 4
BACKWARD found at position (1, 9)

This is word: VERTICAL and this is i 5
VERTICAL found at position (0, 1)

This is word: DIAGONAL and this is i 6
DIAGONAL found at position (8, 6)

This is word: WIKIPEDIA and this is i 7
WIKIPEDIA found at position (9, 3)

This is word: HORIZONTAL and this is i 8
HORIZONTAL was not found

This is word: WORDSEARCH and this is i 9
WORDSEARCH was not found

这些是它找不到的词:

    SLEUTH is at position (9,4) 
HORIZONTAL is at position (9,0)
WORDSEARCH is at position (0,0)

非常感谢任何提示、技巧或想法!

最佳答案

在你的检查方法中:

//check the boundaries, if we go out then we didn't find the word;
if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;

正在检查您在增加 x,y 值后是否超出了拼图边界。问题在于它找不到的三个单词,它找到了最后一个字母,递增你的计数器,然后确定你已经超出了拼图的边缘并返回 false,即使它已经找到了最后一个字母。

尝试将条件移动到循环的开头,如下所示:

for (int i = 0; i < word.length(); i++){
//check the boundaries, if we go out then we didn't find the word;
if (x < 0 || x >= puzzle.length || y < 0 || y >= puzzle[x].length) return false;

char c = word.charAt(i);

//Is not equal
if (puzzle[x][y] != c) return false;

x += offsetx;
y += offsety;
}

关于java - 循环并不能处理所有事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19128624/

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