I cannot find my exception although the errors are at
虽然有错误,但我找不到我的例外。
at puzzle.Puzzle.isSafe(Puzzle.java:97)
at puzzle.Puzzle.main(Puzzle.java:49)
Java Result: 1
I need to print a 15x 15 grid with random words in 6 directions and starts in empty spaces.
我需要打印一个15x15的网格,随机的单词在6个方向,并开始在空白的空间。
My java code is as follows.
我的java代码如下。
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package puzzle;
//import java.util.Arrays;
import java.util.Random;
/**
*
* @author sony
*/
public class Puzzle {
static char[][] grid;
//class solve= new Puzzle();
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
grid =new char[15][15];
String[] words={"hello","coward","heartbeat","beautiful","kind"};
String[] direction = {"horizontal","horizontalBack","vertical","varticalUp",
"diagonal","diagonalBack"};
int i,j,x,y,dir;
Random randGen = new Random();
for(i=0; i<15 ;i++)
{
for(j=0;j<15;j++)
grid[i][j]='*';
}
for(i=0;i< words.length;i++)
{
int set=0;
while(set!=1)
{
x = randGen.nextInt(15);
y = randGen.nextInt(15);
dir = randGen.nextInt(6);
if((isSafe(x,y,words[i],direction[dir])))
{
place(x,y,words[i],direction[dir]);
set=1;
}
}
}
for(i=0; i<15; i++)
{
for(j=0;j<15;j++)
System.out.print(grid[i][j]);
System.out.println("");
}
}
static boolean isSafe(int x,int y, String word, String d)
{ int len=word.length();
int i,k,j;
if(d.equals("horizontal"))
for(i=y,k=0;i< (y+len);i++,k++)
{
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y+len) >15) )
return false;
}
if(d.equals("horizontalBack"))
for(i=y,k=0;i >(y-len);i--,k++)
{
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y-len) <0) )
return false;
}
if(d.equals("vertical"))
for(i=x,k=0;i <(x+len);i++,k++)
{
if((grid[i][y]!='*')&& (grid[i][y]!=word.charAt(k)) && ((x+len) >15) )
return false;
}
if(d.equals("verticalUp"))
for(i=x,k=0;i >(x+len);i++,k++)
{
if((grid[i][y]!='*')&& (grid[i][y]!=word.charAt(k)) && ((x-len) <0) )
return false;
}
if(d.equals("diagonal"))
{ k=0;i=y;j=x;
while((i< (y+len)) && (j< x+len)) {
if((grid[i][j]!='*')&& (grid[i][j]!=word.charAt(k)) && ((x+len) >15) && ((y+len)>15) )
{return false;}
i++;j++;k++;
}
}
if(d.equals("diagonalBack"))
{ k=0;i=y;j=x;
while((i> (y-len)) && (j>x-len)) {
if((grid[i][j]!='*')&& (grid[i][j]!=word.charAt(k)) && ((x-len)<0) && ((y-len)<0) )
{return false;}
i--;j--;k++;
}
}
return true;
}
static void place(int x, int y, String word, String d)
{ int len = word.length();
int i,k,j;
if(d.equals("horizontal"))
for( i=y, k=0;i< (y+len);i++,k++)
{
grid[x][i]=word.charAt(k);
}
if(d.equals("horizontalBack"))
for( i=y,k=0;i> (y-len);i--,k++)
{
grid[x][i]=word.charAt(k);
}
if(d.equals("vertical"))
for( i=x,k=0;i< (x+len);i++,k++)
{
grid[i][y]=word.charAt(k);
}
if(d.equals("verticalUp"))
for( i=x,k=0;i> (x-len);i--,k++)
{
grid[i][y]=word.charAt(k);
}
if(d.equals("diagonal"))
{ i=y;j=x;k=0;
while((i< (y+len)) && (j< (x+len)))
{
grid[i][j]=word.charAt(k);
i++;j++;k++;
}
}
if(d.equals("diagonalUp"))
{ i=y;j=x;k=0;
while((i> (y-len)) && (j> (x-len)))
{
grid[i][j]=word.charAt(k);
i--;j--;k++;
}
}
}
}
4 个解决方案
#1
2
Instead of telling you how to fix your code, I'll help you understand the error.
我将帮助您理解错误,而不是告诉您如何修复代码。
Log
日志
at puzzle.Puzzle.isSafe(Puzzle.java:97)
at puzzle.Puzzle.main(Puzzle.java:49)
Java Result: 1
Puzzle.java:97
Puzzle.java:97
if (grid[i][j]!='*' && grid[i][j]!=word.charAt(k) && (x+len)>15 && (y+len)>15)
Exception
异常
Exception java.lang.ArrayIndexOutOfBoundsException
means that either:
异常. lang。ArrayIndexOutOfBoundsException意味着:
-
i >= grid.length
我> = grid.length
-
j >= grid[i].length
j > =网格[我]. length
Detail
细节
grid.length
and grid[i].length
are determined when the array is created:
网格。长度和网格[我]。在创建数组时确定长度:
grid = new char[15][15];
grid.length is 15
grid[i].length is 15
Moreover
此外
Also, there is the string word
:
还有一个字串:
for (i=y, k=0; i < (y+len); i++, k++)
word.charAt(k);
This will cause an issue too, when k
at some point becomes k >= word.length()
so that loop should be like this:
这也会引起一个问题,当k在某个点变成k >= word.length()时,这个循环应该是这样的:
for (i=y, k=0; i < (y+len) && k < word.length(); i++, k++)
word.charAt(k);
#2
0
I can see two bugs.
我能看到两个虫子。
(1) The line that reads
(1)读取的行。
for(i=x,k=0;i >(x+len);i++,k++)
should read
应该阅读
for(i=x,k=0;i >(x-len);i--,k++)
I think it's line 89.
我想是第89行。
(2) The bug that's causing this exception is the fact that every time you've got a whole lot of conditions separated by &&
, the first one should be &&
and the subsequent ones should be ||
.
(2)导致这个异常的错误是,每次你有很多条件被分开时,第一个应该是&&并且后面的应该是||。
For example,
例如,
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y+len) >15)) {
should read
应该阅读
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) || ((y+len) >15)) {
because you need to return that this square is an unsafe place to start the word if y + len > 15
regardless of what is found at each of the grid squares that you're checking. Similarly, with all the other conditions that look like this.
因为你需要返回这个正方形是一个不安全的地方来开始这个词如果y + len > 15不管你检查的每个网格的平方是什么。类似地,所有其他条件都是这样的。
#3
0
Please note the typo. varticalUp
in the array declaration and verticalUp
in the if condition. Although this is not the cause of the problem but its worth noting it.
请注意输入错误。在if条件下,在数组声明和verticalUp中显示。虽然这不是问题的原因,但值得注意。
#4
-1
I just quickly went through your code and it seems that the problem is in your variable len
. See the statement int len=word.length();
on line 68. In fact it should be int len=word.length()-1;
because the array index starts from 0
. I have not tested it, but you can give it a try.
我只是快速浏览了一下你的代码,似乎问题就在你的变量len中。查看语句int =word.length();在第68行。实际上它应该是int len=word.length()-1;因为数组索引从0开始。我还没试过,不过你可以试一试。
#1
2
Instead of telling you how to fix your code, I'll help you understand the error.
我将帮助您理解错误,而不是告诉您如何修复代码。
Log
日志
at puzzle.Puzzle.isSafe(Puzzle.java:97)
at puzzle.Puzzle.main(Puzzle.java:49)
Java Result: 1
Puzzle.java:97
Puzzle.java:97
if (grid[i][j]!='*' && grid[i][j]!=word.charAt(k) && (x+len)>15 && (y+len)>15)
Exception
异常
Exception java.lang.ArrayIndexOutOfBoundsException
means that either:
异常. lang。ArrayIndexOutOfBoundsException意味着:
-
i >= grid.length
我> = grid.length
-
j >= grid[i].length
j > =网格[我]. length
Detail
细节
grid.length
and grid[i].length
are determined when the array is created:
网格。长度和网格[我]。在创建数组时确定长度:
grid = new char[15][15];
grid.length is 15
grid[i].length is 15
Moreover
此外
Also, there is the string word
:
还有一个字串:
for (i=y, k=0; i < (y+len); i++, k++)
word.charAt(k);
This will cause an issue too, when k
at some point becomes k >= word.length()
so that loop should be like this:
这也会引起一个问题,当k在某个点变成k >= word.length()时,这个循环应该是这样的:
for (i=y, k=0; i < (y+len) && k < word.length(); i++, k++)
word.charAt(k);
#2
0
I can see two bugs.
我能看到两个虫子。
(1) The line that reads
(1)读取的行。
for(i=x,k=0;i >(x+len);i++,k++)
should read
应该阅读
for(i=x,k=0;i >(x-len);i--,k++)
I think it's line 89.
我想是第89行。
(2) The bug that's causing this exception is the fact that every time you've got a whole lot of conditions separated by &&
, the first one should be &&
and the subsequent ones should be ||
.
(2)导致这个异常的错误是,每次你有很多条件被分开时,第一个应该是&&并且后面的应该是||。
For example,
例如,
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) && ((y+len) >15)) {
should read
应该阅读
if((grid[x][i]!='*')&& (grid[x][i]!=word.charAt(k)) || ((y+len) >15)) {
because you need to return that this square is an unsafe place to start the word if y + len > 15
regardless of what is found at each of the grid squares that you're checking. Similarly, with all the other conditions that look like this.
因为你需要返回这个正方形是一个不安全的地方来开始这个词如果y + len > 15不管你检查的每个网格的平方是什么。类似地,所有其他条件都是这样的。
#3
0
Please note the typo. varticalUp
in the array declaration and verticalUp
in the if condition. Although this is not the cause of the problem but its worth noting it.
请注意输入错误。在if条件下,在数组声明和verticalUp中显示。虽然这不是问题的原因,但值得注意。
#4
-1
I just quickly went through your code and it seems that the problem is in your variable len
. See the statement int len=word.length();
on line 68. In fact it should be int len=word.length()-1;
because the array index starts from 0
. I have not tested it, but you can give it a try.
我只是快速浏览了一下你的代码,似乎问题就在你的变量len中。查看语句int =word.length();在第68行。实际上它应该是int len=word.length()-1;因为数组索引从0开始。我还没试过,不过你可以试一试。