The method isFilledAt()
returns true
if the shape has a filled block at the given row/col
position and false
if the block is empty. If the position is out of bounds, raise a FitItException
with an informative message. Im running a nested loop to get the position and having trouble how to figure out if a position is out of bounds. Could smb help out? Thanks in advance!
如果在给定的行/col位置上有一个填充的块,如果块为空,则该方法将返回true。如果该位置超出了范围,请使用一个信息丰富的FitItException。我正在运行一个嵌套循环来获取位置,并且不知道如何判断一个位置是否超出了界限。smb可以帮助吗?提前谢谢!
public class CreateShape {
private int height;
private int width;
private char dc;
private Rotation initialPos;
public CreateShape(int height, int width, char dc)
{
this.height = height;
this.width = width;
this.dc = dc;
initialPos = Rotation.CW0;
}
public boolean isFilledAt(int row, int col)
{
char[][] tempArray = new char[height][width];
for(int i = 0; i < tempArray.length; i++)
for(int j = 0; j < tempArray[i].length; j++)
{
if(row > tempArray.length || row < 0)
throw new FitItException("Out of Bounds!");
if(tempArray[row][col] == dc)
return true;
}
return false;
}
2 个解决方案
#1
3
You need to check if the row
or col
are less than zero, or if row
is greater than or equal to the height
, or if col
is greater than or equal to the width
. Note you only need to do the validation once, so you could move the check outside the loop:
您需要检查行或col是否小于零,行是否大于或等于高,或者col是否大于或等于宽。注意,您只需要执行一次验证,因此可以将检查移出循环:
public boolean isFilledAt(int row, int col) {
if (row < 0 || row >= height || col < 0 || col >= width) {
throw new FitItException("Out of Bounds!");
}
char[][] tempArray = new char[height][width];
for (int i = 0; i < tempArray.length; i++) {
for (int j = 0; j < tempArray[i].length; j++) {
if (tempArray[row][col] == dc) {
return true;
}
}
}
return false;
}
Note however that isFilledAt()
may not work as you intend. Since you recreate tempArray
every time you call the method, the condition tempArray[row][col] == dc
will probably never evaluate to true
.
但是请注意,isFilledAt()可能不能按照您的意愿工作。由于每次调用该方法时都要重新创建tempArray,因此条件tempArray[row][col] = dc可能永远不会计算为true。
#2
1
You should change your isFilledAt method as follows
您应该更改isFilledAt方法,如下所示
public boolean isFilledAt(int row, int col)
{
char[][] tempArray = new char[height][width];
// Calls the method that fills tempArray datas
if ( (row >= height || row <0) || (col >= width || col < 0)) {
throw new FitItException("Out of Bounds!");
}
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
{
if(tempArray[row][col] == dc)
return true;
}
}
return false;
}
You place your return false at the wrong place: it would systematically return false if the looked up value was not inside the first row. Instead do a return false after every row has been iterated
您将return false放置在错误的位置:如果查找的值不在第一行中,则系统地返回false。相反,在遍历每一行之后执行返回false
#1
3
You need to check if the row
or col
are less than zero, or if row
is greater than or equal to the height
, or if col
is greater than or equal to the width
. Note you only need to do the validation once, so you could move the check outside the loop:
您需要检查行或col是否小于零,行是否大于或等于高,或者col是否大于或等于宽。注意,您只需要执行一次验证,因此可以将检查移出循环:
public boolean isFilledAt(int row, int col) {
if (row < 0 || row >= height || col < 0 || col >= width) {
throw new FitItException("Out of Bounds!");
}
char[][] tempArray = new char[height][width];
for (int i = 0; i < tempArray.length; i++) {
for (int j = 0; j < tempArray[i].length; j++) {
if (tempArray[row][col] == dc) {
return true;
}
}
}
return false;
}
Note however that isFilledAt()
may not work as you intend. Since you recreate tempArray
every time you call the method, the condition tempArray[row][col] == dc
will probably never evaluate to true
.
但是请注意,isFilledAt()可能不能按照您的意愿工作。由于每次调用该方法时都要重新创建tempArray,因此条件tempArray[row][col] = dc可能永远不会计算为true。
#2
1
You should change your isFilledAt method as follows
您应该更改isFilledAt方法,如下所示
public boolean isFilledAt(int row, int col)
{
char[][] tempArray = new char[height][width];
// Calls the method that fills tempArray datas
if ( (row >= height || row <0) || (col >= width || col < 0)) {
throw new FitItException("Out of Bounds!");
}
for(int i = 0; i < height; i++)
for(int j = 0; j < width; j++)
{
if(tempArray[row][col] == dc)
return true;
}
}
return false;
}
You place your return false at the wrong place: it would systematically return false if the looked up value was not inside the first row. Instead do a return false after every row has been iterated
您将return false放置在错误的位置:如果查找的值不在第一行中,则系统地返回false。相反,在遍历每一行之后执行返回false