我正在编写一个程序,根据用户输入的矩形和角坐标识别哪些矩形相互交叉。我正在尝试创建一个for循环,检查每个矩形,矩形(由用户输入)与它相交。在for循环中,我试图让程序检查一个矩形对每个其他矩形,看看它们是否相交并尝试按如下方式执行:
package a1;
import java.util.Scanner;
public class A1Jedi {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
process(s);
}
public static void process(Scanner s) {
String[] rectnames;
int[] xcoordinate1;
int[]ycoordinate1;
int[]xcoordinate2;
int[]ycoordinate2;
int[]xcoordinate3;
int[]ycoordinate3;
int[]xcoordinate4;
int[]ycoordinate4;
String[] intersectrectnames;
System.out.println("Please enter the number of rectangles as an integer");
int rectnum;
rectnum = s.nextInt();
rectnames = new String[rectnum];
xcoordinate1 = new int[rectnum];
ycoordinate1 = new int[rectnum];
xcoordinate2 = new int[rectnum];
ycoordinate2 = new int[rectnum];
xcoordinate3 = new int[rectnum];
ycoordinate3 = new int[rectnum];
xcoordinate4 = new int[rectnum];
ycoordinate4 = new int[rectnum];
intersectrectnames = new String[rectnum-1];
for(int i = 0; i<rectnum; i++){
System.out.println("PLease enter the rectangle's name as a single letter");
String rectname;
rectname = s.next();
rectnames[i] = rectname;
System.out.println("Please enter the x value of one coordinate of the rectangle" );
int xcoor1;
xcoor1 = s.nextInt();
xcoordinate1[i]= xcoor1;
System.out.println("Please enter the y value of the same coordinate of the rectangle");
int ycoor1;
ycoor1 = s.nextInt();
ycoordinate1[i]= ycoor1;
System.out.println("Please enter the x value of annother coordinate of the rectangle");
int xcoor2;
xcoor2 = s.nextInt();
xcoordinate2[i]= xcoor2;
System.out.println("Please enter the y value of the same coordinate of the rectangle");
int ycoor2;
ycoor2 = s.nextInt();
ycoordinate2[i]= ycoor2;
}
for(int a=0; a < rectnum; a++) {
for(int b=a+1; b < rectnum; b++) {
if ((xcoordinate1[a]< xcoordinate2[b]) && (xcoordinate2[a] > xcoordinate1[b])
&& (ycoordinate1[a] < ycoordinate2[b]) && (ycoordinate2[a] > ycoordinate1[b])){
intersectrectnames[a] = rectnames[b];
System.out.println(intersectrectnames[a]);
}
}
}}}
答案 0 :(得分:3)
您的代码每个循环递减rectnum
五次。它已经发生在每次迭代的循环顶部。操作--rectnum
更改 rectnum
的值。
for(int i = 0; i < --rectnum; i++) { // pre-decrement here
if ((xcoordinate1[i] < xcoordinate2[rectnum]) &&
(xcoordinate2[i] > xcoordinate1[rectnum]) &&
(ycoordinate1[i] < ycoordinate2[rectnum]) &&
(ycoordinate2[i] > ycoordinate1[rectnum])) {
intersectrectnames[i] = rectnames[rectnum];
}
}
要像你一样拥有它五次,而不更改它,代码就是这样(可能,我不知道完整代码的作用。):
for(int i = 0; i < rectnum; i++) {
if ((xcoordinate1[i] < xcoordinate2[rectnum-1]) &&
(xcoordinate2[i] > xcoordinate1[rectnum-1]) &&
(ycoordinate1[i] < ycoordinate2[rectnum-1]) &&
(ycoordinate2[i] > ycoordinate1[rectnum-1])) {
intersectrectnames[i] = rectnames[rectnum-1];
}
}
答案 1 :(得分:0)
你需要两个循环,因为你想要穿过成对的矩形。在这种情况下,内循环从已经比较的内循环开始,以避免比较它们两次:
int recnum = xcoordinate1.length; // or any other way
for(int a=0; a < recnum; a++) {
for(int b=a+1; b < recnum; b++) {
if ((xcoordinate1[a] < xcoordinate2[b] ...)
{
System.out.println("rectangle " + a + " and " + b + " intersect");
}
}
}
这比较(如果你有4个矩形)
a=0 with b=1, b=2, b=3
a=1 with b=2, b=3
a=2 with b=3
外部循环也将尝试比较a = 3,但内部循环随后以b=3+1
开始,且不小于4,因此不再执行。
你最初的问题是关于循环变量,所以我跳过if逻辑。但是,您还需要改进if代码,它必须检查所有不同情况下如何相对于彼此(以及两个维度)放置重叠。提示:if至少有2&amp;&amp;包含多个ors的组合表达式。您可以在一个更大的声明中进行,该声明考虑了所有不同的组合(谁更左和更多),或者您首先指定最左侧和最多的组合,并考虑到这一事实。