java中的Wordhunt程序
这就是它所做的。。。 我有一个.txt文件,我已经存储在一个二维数组中,它可以工作。。。 这就是我应该在表中搜索一个单词的问题,我想不出一种方法来比较数组中的字母并返回它的索引。。。。(因此,基本上我想在2d数组中找到一个字母,并希望它返回其索引int c[]]) 真正的作业就像一个文字搜索拼图(在这里它可以找到水平垂直对角线(左和右)以及向后(在所说的方向),但我很确定,一旦我知道如何获得我搜索的单词的2st字母的索引,我就可以绕过它了java中的Wordhunt程序,java,Java,这就是它所做的。。。 我有一个.txt文件,我已经存储在一个二维数组中,它可以工作。。。 这就是我应该在表中搜索一个单词的问题,我想不出一种方法来比较数组中的字母并返回它的索引。。。。(因此,基本上我想在2d数组中找到一个字母,并希望它返回其索引int c[]]) 真正的作业就像一个文字搜索拼图(在这里它可以找到水平垂直对角线(左和右)以及向后(在所说的方向),但我很确定,一旦我知道如何获得我搜索的单词的2st字母的索引,我就可以绕过它了 import java.io.*; import jav
import java.io.*;
import java.util.Scanner;
public class Mp6 {
public static void main(String[] args) throws IOException {
FileReader a = new FileReader("data/Data.txt");
BufferedReader ss = new BufferedReader(a);
char wla[][];
int x = 80;
int y = 80;
wla = new char [x][y];
String d;
Scanner p = new Scanner(System.in);
int c = 0;
int n = 0 ;
try {
for( c = 0; c < x; c++){
d = ss.readLine();
for ( n = 0; n<y;n++){
wla[c][n]=d.charAt(n);
System.out.print(wla[c][n]);
}
System.out.println("");
}
System.out.println("Search for:");
String h = p.nextLine();
h=h.toUpperCase();
char [] ph = new char[h.length()];
int counter = 0;
for(counter = 0 ; counter < h.length();counter++){
ph[counter]=h.charAt(counter);
System.out.print(ph[counter]);
}
//horizontal
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
import java.io.*;
导入java.util.Scanner;
公共级Mp6{
公共静态void main(字符串[]args)引发IOException{
FileReader a=新的FileReader(“data/data.txt”);
BufferedReader ss=新的BufferedReader(a);
字符wla[][];
int x=80;
int y=80;
wla=新字符[x][y];
字符串d;
扫描仪p=新扫描仪(系统英寸);
int c=0;
int n=0;
试一试{
对于(c=0;c 对于(n=0;n如果我理解正确,您希望在所有方向的2d数组中搜索单词。在这种情况下,最好的算法应该是这样的:
循环查找数组
单词的第一个字母
找到一个后-在所有搜索中
其余字母的说明
这是如何做到这一点的示例:
char[] word = getWordToFind();
char[][] array = getArrayWhereToSearch();
//Search for first letter of the word
for (int x = 0; x<array.length;x++) {
for (int y=0; y<array[x].length;y++) {
//If the first letter is found
if (array[x][y] == word[0]) {
//Search in all directions
if (searchEast(array, word, x, y)) {
println(String.format("Word match found! X: %d, Y: %d, direction: '%s'", x, y, "east"));
}
if (searchSouth(array, word, x, y)) {
println(String.format("Word match found! X: %d, Y: %d, direction: '%s'", x, y, "south"));
}
//More directions if needed
...
}
}
}
public boolean searchEast(char[][] array, char[] word, int startX, int startY) {
//If the word can't fit to the right
if (x >= array.length-word.length) {
return false;
}
//Search the remaining letters to the right starting from startX+1
for (int dx = 1; dx<word.length;dx++) {
if (array[startX+dx] != word[dx]) { //If the letter mismatches
return false;
}
}
//All letters matched
return true;
}
public boolean searchSouth(char[][] array, char[] word, int startX, int startY) {
//If the word can't fit to the bottom
if (y >= array[startX].length-word.length) {
return false;
}
//Search the remaining letters to the bottom starting from startY+1
for (int dy = 1; dy<word.length;dy++) {
if (array[startX][startY+dy] != word[dy]) { //If the letter mismatches
return false;
}
}
//All letters matched
return true;
}
char[]word=getWordToFind();
char[][]数组=GetArrayWhere搜索();
//搜索单词的第一个字母
对于(int x=0;x=array[startX].length word.length){
返回false;
}
//从startY+1开始搜索底部剩余的字母
对于(int dy=1;dy这与javascript有什么关系?这与javascript有什么关系?如果不相关,请编辑并删除标记,这很混乱..thx我得到了我的索引,,不管怎样,你是自己编写的还是有原始链接?如果有原始链接,我可以拥有吗?