在过去的几天里,我一直在为大学做一个 Java 项目。我遇到了一些问题,在本网站用户的帮助下我克服了这些问题。我正在做的是单词搜索,它将用户输入的单词作为字符串的数组列表,并将它们随机映射到二维数组上。我已经创建了用于选择行和列、选择单词的方向以及检查是否有足够的空白空间用于所述单词的方法。我在编译该代码时遇到了一些错误。这是我之前的问题,所以你可以看到,从那时起我已经取得了一些进展:) https://stackoverflow.com/questions/10193291/adding-words-to-a-2-d-array
具体来说,问题是我试图通过使用随机数来决定选择哪种方法来引用 doitfit 方法(上、下、左或右)。它没有编译:/
import java.io.* ;
import java.util.ArrayList ;
public class WordSearchPuzzle
{
private char[][] puzzle ;
private ArrayList<String> puzzleWords ;
private int letterCount = 0 ;
private int gridDimensions;
public static int row, column;
public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
{
this.puzzleWords = userSpecifiedWords ;
}
private void createPuzzleGrid()
{
int i, itemLength;
String item;
for (i = 0; i < puzzleWords.size(); i++) {
item = puzzleWords.get(i);
itemLength = item.length();
letterCount = letterCount + itemLength;
}
gridDimensions = letterCount * 2;
puzzle = new char[gridDimensions][gridDimensions] ;
}
private void generateWordSearchPuzzle()
{
}
public void northSouthEastWest(String word)
{
int upDownLeftRight, north, south, east, west;
north = 1;
south = 2;
east = 3;
west = 4;
String Word;
upDownLeftRight = (int)(Math.random() * 4);
if(upDownLeftRight == north){
fitWordNorth(word);
}else if(upDownLeftRight == south){
fitWordSouth(word);
}else if(upDownLeftRight == east){
fitWordEast(word);
}else if(upDownLeftRight == west){
fitWordWest(word);
}
}
public void firstSpace(String word)
{
row = (int)(Math.random() * gridDimensions);
column = (int)(Math.random() * gridDimensions);
if(puzzle[row][column] != ' ') {
firstSpace(word);
} else {
northSouthEastWest(word);
}
}
public void fitWordNorth(String word)
{
boolean clear = false;
int p, i;
if(row >= word.length()){
for(i = row - 1; i < word.length(); i--){
if(puzzle[i][column] != ' '){
firstSpace(word);
}else{
clear = true;
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row - p][column] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
}
}
public void fitWordSouth(String word)
{
boolean clear = false;
int row, column, p, i;
if(row >= word.length()){
for(i = row + 1; i < word.length(); i++){
if(puzzle[i][column] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row + p][column] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
public void fitWordWest(String word)
{
boolean clear = false;
int row, column, p, i;
if(column >= word.length()){
for(i = column - 1; i < word.length(); i--){
if(puzzle[row][i] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row][column - p] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
public void fitWordEast(String word)
{
boolean clear = false;
int row, column, p, i;
if(column >= word.length()){
for(i = column + 1; i < word.length(); i++){
if(puzzle[row][i] != ' '){
firstSpace(word);
}
clear = true;
}
}else{
firstSpace(word);
}
if(clear == true){
for(p = 0; p < word.length(); p++){
puzzle[row][column + p] = word.charAt(p);
}
}else{
firstSpace(word);
}
}
}