import java.util.Scanner;
public class ex8 {
public static boolean inRect(int x,int y, int rectx1,int recty1, int rectx2, int recty2) {
if((x>=rectx1 && x<=rectx2)&&(y>=recty1 && y<=recty2)) return true;
else return false;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
System.out.print("점 (x1,y1), (x2,y2)의 좌표를 입력하시오>>");
int x1 = sc.nextInt();
int y1 = sc.nextInt();
int x2 = sc.nextInt();
int y2 = sc.nextInt();
if(inRect(x1,y1,100,100,200,200)&&inRect(x2,y2,100,100,200,200)) {
System.out.println("직사각형과 충돌하지 않는다.");
}
else if((inRect(x1,y1,100,100,200,200)==true&&inRect(x2,y2,100,100,200,200)==false)||(inRect(x1,y1,100,100,200,200)==false&&inRect(x2,y2,100,100,200,200)==true)) {
System.out.println("직사각형과 충돌한다.");
}
else {
System.out.println("직사각형과 충돌하지 않는다.");
}
sc.close();
}
}
결과
점 (x1,y1), (x2,y2)의 좌표를 입력하시오>>400 400 500 500
직사각형과 충돌하지 않는다.
점 (x1,y1), (x2,y2)의 좌표를 입력하시오>>250 250 100 100
직사각형과 충돌한다.
점 (x1,y1), (x2,y2)의 좌표를 입력하시오>>150 150 180 180
직사각형과 충돌하지 않는다.'명품JAVA프로그래밍 > 2장 자바 기본 프로그래밍' 카테고리의 다른 글
| [명품JAVA프로그래밍] 2장 실습문제 10번 (0) | 2021.12.16 |
|---|---|
| [명품JAVA프로그래밍] 2장 실습문제 9번 (0) | 2021.12.16 |
| [명품JAVA프로그래밍] 2장 실습문제 7번 (0) | 2021.12.15 |
| [명품JAVA프로그래밍] 2장 실습문제 6번 (0) | 2021.12.15 |
| [명품JAVA프로그래밍] 2장 실습문제 5번 (0) | 2021.12.15 |