我試圖讓一個程序來查找在Java中的單詞搜索詞。我知道這件事已經完成,但我仍然在學習,如果我自己做,它會更酷。無論如何,我發現我掃描網格的主要for循環只執行我想要查找的第一個單詞。下面是找到這個詞我的代碼的一部分:For循環只能第一次工作
while (!word.equalsIgnoreCase("/exit")) {
{ //Find word
Pair cursor = new Pair();
Pair direction = new Pair();
Pair location = new Pair();
for (int x = 0; !whole_word_found && x<grid.length; x++) { int y = 0;
for (y = 0; !whole_word_found && y<grid[x].length; y++) {
cursor = new Pair(x,y);
//Lots of word-finding code, including writing whole_word_found and using the three pairs listed above
}
}
//Print location of word
if (word.length() != 1) {
if (whole_word_found) {
System.out.printf("The word is located at %s going %s.%n",location.toString(0),direction.toString(0));
} else {
System.out.println("Sorry, word not found.");
}
}
} //Find word
//Get next word
System.out.println("Enter another word, or type \"/exit\" to exit.");
input = new Scanner(System.in);
word = input.nextLine().replaceAll(" ","");
}
所有的變量都正確初始化,並且對是一類我掀起了舉辦一個有序對(X,Y)的值。我只是不喜歡爲每一對我必須做的x-y對分配兩個值(儘管我在技術上仍然這樣做)。
無論如何,感謝您的幫助,如果你能找到它。
當你在調試器中遍歷代碼時,你會看到什麼? –
你可以發佈周邊的代碼嗎?你爲什麼從'word'中刪除空格?什麼是'whole_word_found'? –
你是否將'whole_word_found'重置爲false? – Axarydax