一个重要的类,Recktangle,可求交集也可求并集
import java.awt.Rectangle; import java.math.BigInteger; import java.sql.Date; import java.util.*; public class Main1 { public static Rectangle getrec(Rectangle []rec){ Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] ss = s.split(","); int x1 = Integer.parseInt(ss[0]); int y1 = Integer.parseInt(ss[1]); int x2 = Integer.parseInt(ss[2]); int y2 = Integer.parseInt(ss[3]); return new Rectangle(Math.min(x1, x2),Math.min(y1, y2),Math.abs(x1-x2),Math.abs(y1-y2)); } public static void op(Rectangle rec[]){ Rectangle rr,r; if(rec[0].intersects(rec[1])){ r=rec[0].intersection(rec[1]); System.out.println(r.x+","+r.y+","+r.width+","+r.height); }else System.out.println("不存在"); rr=rec[0].union(rec[1]); System.out.println(rr.x+","+rr.y+","+rr.width+","+rr.height); } public static void main(String[] args) {Rectangle rec[] = new Rectangle[2]; rec[0] = getrec(rec); // 第一个矩形 rec[1] = getrec(rec); // 第二个矩形 op(rec); // 输出 } }