是否有可能将以下循环重写为单行循环?
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].equals(points[j])) {
return "Two vertices coincide therefore a polygon can't be formed";
}
}
}
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].isOnLineSegment(points[j], points[j + 1])) {
return "There's a vertex on an edge therefore a polygon can't be formed";
}
}
}
我想编写发布条件,但除了可能将循环重写为一行之外,我看不到如何做到这一点。
您是否在寻找:
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
if (points[i].equals(points[j])) {
return "Two vertices coincide therefore a polygon can't be formed";
}
if (points[i].isOnLineSegment(points[j], points[j + 1])) {
return "There's a vertex on an edge therefore a polygon can't be formed";
}
}
}