본문 바로가기
코딩/Java

명품 java programming 2장 실습문제 8번

by developfactory 2020. 9. 19.
import java.util.Scanner;

public class InputRectangle {
	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[]) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("점 (rectx1,recty1)의 좌표를 입력하시오>>");
		int rectx1 = scanner.nextInt();
		int recty1 = scanner.nextInt();
		System.out.print("점 (rectx2,recty2)의 좌표를 입력하시오>>");
		int rectx2 = scanner.nextInt();
		int recty2 = scanner.nextInt();
		System.out.print("점 (x, y)의 좌표를 입력하시오>>");
		int x = scanner.nextInt();
		int y = scanner.nextInt();
		System.out.print("(" + x + "," + y + ")는 사각형 안에 ");
		if(inRect(x,y, rectx1,recty1,rectx2,recty2)) // 함수호출시 참값이면 실행(x,y좌표 직사각형범위에 둘다 포함될 경우)
			System.out.print("있습니다.");
		else // 함수호출시 거짓일시 실행(x,y좌표 직사각형범위에 둘다 포함 안될 경우)
			System.out.print("없습니다.");
		scanner.close(); 
	}
}

실행결과

댓글