2D配列にランダムな文字を入力してから、ユーザが入力した単語を2D配列に挿入します。それらを水平方向、垂直方向、斜め方向に追加することになっています。正しく動作させるには、垂直方向に単語を追加するしかありません。Java:動作していない文字配列に単語を配置するプログラム
または少なくとも、私は出力が正しく出力されることを何度か求めてから、斜めに終わることを意図しています。理由はわかりません。私はちょうど垂直のものの代わりに斜めのバリエーションとしてこれをラベルすることができると思うが、私はまだこれが起こっている理由を理解したいと思います。
また、現在の方法では、単語を追加するときにインデックスが境界外にある場合、単語を追加する別の位置を探しますが、そこに半分の単語が残ります。私はそれが完全に合っていれば、単語を出力するだけです。時にはそれは良い場所を見つけることは決してないと私はStackOverflowErrorで終わる。ここで
は、いくつかのサンプル入力/出力されます: - :
Before adding:
w a y c c
z l l z u
l c c a v
d h l z d
u d n c h
After adding:
w W y c c
z W O z u
l c O R v
d h l R D
u d n c D
あるいは:
Input:
How many rows would you like? >
5
How many columns would you like? >
5
Add a word to your search(Type end to stop) >
WORD
Add a word to your search(Type end to stop) >
end
同じ出力を持つ
Output:
Before adding:
g m g m c
a t j z l
m z t d u
q o f e q
c p g s g
After adding:
g m g m c
a W j z l
m z O d u
q o f R q
c p g s D
またはいくつかのケースでは
Before adding:
h d o x r
c r t p b
n e r p a
a q m s q
s r d l h
After adding:
W d o x r
c O W p b
n e R O a //Here there wasn't enough space for WORD
a q m D R //So it got to WOR and then looked for a new start point
s r d l h
そしてHERESに私のコード:
import java.util.Scanner;
import java.util.ArrayList;
public class BuildWS
{
private char [][]board;
private int ROW, COL;
private String inWord;
ArrayList<String> words = new ArrayList<String>();
public void build()
{
Scanner input = new Scanner(System.in); //takes in user input
System.out.println("How many rows would you like? >");
ROW = input.nextInt();
System.out.println("How many columns would you like? >");
COL = input.nextInt();
input.nextLine();
do{
System.out.println("Add a word to your search (end to stop) >");
inWord = input.nextLine();
if(!inWord.equals("end"))
{
words.add(inWord);
}
}while(!inWord.equals("end"));
fillBoard();
System.out.println("Before adding:");
printBoard();
addWords();
System.out.println("After adding:");
printBoard()
}
public void fillBoard()
{
board = new char[ROW][COL];
for(int rows = 0; rows < board.length; rows++)
{
for(int cols = 0; cols < board[rows].length; cols++)
{
board[rows][cols] = randomChar();
}
}
}
public void addWords()
{
int rowPos, colPos, orientation;
for(String s: words)
{
rowPos = (int)(Math.random() * ROW);
colPos = (int)(Math.random() * COL);
orientation = 1; //(int)(Math.random() * 2); // 0 = horizontal, 1 = vertical
if(inBounds(rowPos, colPos))
{
if(orientation == 1) //set to 1 for easy testing
{
for(int i = 0; i < 1; i++) //go into row once
{
for(int j = 0; (j < s.length()); j++) //go through columns to the right
{
if(inBounds(rowPos, colPos)) //if index is in bounds
{
board[rowPos][colPos] = s.charAt(index); //change element in board to current char of current word
rowPos++;
colPos++
}
else
{
addWords();
}
}
}
}
}
else
{
addWords();
}
}
}
public void printBoard()
{
for(int i = 0; i < board.length; i++)
{
for(int j = 0; j < board[i].length; j++)
{
System.out.print(board[i][j] + " ");
}
System.out.println();
}
System.out.println();
}
public boolean inBounds(int inRow, int inCol)
{
return ((inRow >= 0) && (inRow < ROW) && (inCol >= 0) && (inCol < COL));
}
public char randomChar()
{
char alphabet[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z'};
return alphabet[(char)(alphabet.length * Math.random())];
}
}
私は本当に任意の助けをいただければと思います。ありがとう!
デバッガを使用してコードを実行して、何をしているのかを調べることをお勧めします。 –
縦の列に最初からいくつかの文字があるとします。新しい文字を置くと、olfの文字は置き換えられますか? –