Я пишу программу для определения того, какие прямоугольники пересекаются друг с другом на основе прямоугольников и угловых координат, которые вводит пользователь. Я пытаюсь создать цикл for, который проверяет каждый прямоугольник, прямоугольники которого (вход пользователя) пересекают его. В цикле, я пытаюсь сделать программу проверкой один прямоугольника с любым другим прямоугольником, чтобы увидеть, если они пересекаются и пытаюсь сделать это следующим образом:Как вы делаете значение внутри цикла for, которое не является значением i, постепенно уменьшается?
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]);
}
}
}}}
Какая ошибка дает вам? –
Исключение в потоке "основного" java.lang.ArrayIndexOutOfBoundsException: -1 \t в a1.A1Jedi.process (A1Jedi.java:71) \t в a1.A1Jedi.main (A1Jedi.java:7) – user3440080
Вы действительно намерены уменьшать «rectnum» пять раз за цикл? Потому что это то, что ваш код в настоящее время пытается сделать. – eldarerathis