[英]how to determine whether each border side of a string rectangle contains at least one specified character?
Lets say there is a string that represents a rectangle, and this rectangle only contains two types of chars that are '.' 可以说有一个代表矩形的字符串,并且该矩形仅包含两种类型的字符“。”。 and 'x':
和'x':
String layout = "..x.\nx..x\n.x.."
System.out.println(layout) -> ..x.
x..x
.xx.
how can I determine whether each border side of the rectangle has at least one 'x'?(use a boolean method) For example, 如何确定矩形的每个边界边是否至少有一个'x'?(使用布尔方法)例如,
//this one is not illegal, because its left side border does not have an 'x'
..x.
..xx
.xx.
I have put this rectangle in a string[][], which means I have a coordinate for each char in this rectangle. 我已经将这个矩形放在一个string [] []中,这意味着我为这个矩形中的每个字符都有一个坐标。 My idea is using four for-loops to check four sides, but it is kind of redundant.
我的想法是使用四个for循环检查四个边,但这有点多余。 Can anyone give a better idea?
谁能给一个更好的主意? Thanks
谢谢
if I understand well, what you want to do than, You can try this. 如果我不太了解,那么您想做什么,您可以尝试一下。 You have any
String[][]
which holds random Strings and each String must have at least x
in it. 您有任何保存随机字符串的
String[][]
,并且每个String中至少必须包含x
。
str[0] X . . .
str[1] . X . .
str[2] . . X .
str[3] . . . X
now you need to check str[i] > isContainLatterX()
which returns boolean
if there is presence of X
. 现在您需要检查
str[i] > isContainLatterX()
,如果存在X
,则返回boolean
。 For Example : 例如 :
public boolean isContainLatterX(String str){
if(str.contains("X"))
return true;
else
return false;
}
Now, need an another Method which returns the Index
of occurrence of latter X
say indexOfLatterX(String str)
现在,需要另一个方法返回后一个
X
的出现Index
,例如indexOfLatterX(String str)
public int indexOfLatterX(String str){
return str.indexOf("X");
}
Just call both these Methods in Your Loop, and check Latter X is Exists or not, if Not than Generate Error, and skip remaining Progress. 只需在您的循环中调用这两种方法,然后检查是否存在X,如果不是“生成错误”,则跳过剩余的Progress。 if Exists than Check its Index with previous Strings, if there is Matching Index founds than Generate Error and Skip remaining Progress.
如果存在,则使用先前的字符串检查其索引,如果找到匹配的索引,则生成错误并跳过剩余进度。
Can we try, just getting the co-ordinates in form of [x][y], and check that x and y values exist in range of length... Missing values gives the empty lines 我们可以尝试一下,只是获取[x] [y]形式的坐标,并检查x和y值是否在长度范围内...缺少值的行为空
..x.
.x..
...x
x...
The [x][y] list will be (0,2),(1,1),(2,3),(3,0) and must be in range {0,3} [x] [y]列表将为(0,2),(1,1),(2,3),(3,0),并且必须在{0,3}范围内
We check that x and y has values (0-3)
available. 我们检查x和y是否具有可用的值
(0-3)
。 If any value is missing, then the corresponding row or column, don't have an x
如果缺少任何值,则对应的行或列没有
x
Since it represents the border side, you can check for (x,y)
in range {0,length-1}
由于它代表边框,因此您可以检查范围
{0,length-1}
(x,y)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.