코딩 연습장/JAVA

자바 2장 연습문제(조건문)-4

Do아 2021. 3. 18. 09:55
728x90

2021/03/09(화)

2-9) 원의 중심을 나타내는 한 점과 반지름을 실수 값으로 입력받아라. 그리고 실수값으로 다름 점(x,y)를 입력받아 이 점이 원의 내부에 있는지 판별하여 출력하라.

원의 중심과 반지름 입력>>10 10 6.5

점 입력>> 13 13

점(13.0, 13.0)는 원 안에 있다.

2-10)원의 정보를 받기 위해 키보드로부터 원의 중심을 나타내는 한 점과 반지름을 입력받는다. 두 개의 원을 입력받고 두 원이 서로 겹치는지 판단하여 출력하라.

첫번째 원의 중심과 반지름 입력>>10 10 3

두번쨰 원의 중심과 반지름 입력>>12 12 2

두 원은 서로 겹친다.

2-11) 숫자를 입력받아 3~5는 "봄", 6~8은 "여름", 9~11은 "가을" ,12,1,2의 경우 "겨울"을, 그 외 숫자를 입력한 경우 "잘못입력"을 출력하는 프로그램을 작성하라.

달을 입력하세요(1~12)>>9

가을

11-1) if-else문을 이용하여 프로그램을 작성하라.

11-2) switch문을 이용하여 프로그램을 작성하라.

2-12) 사칙연산을 입력받아 계산하는 프로그램을 작성하고자 한다. 연산자는 +,-,*,/의 네 가지로 하고 피연산자는 모두 실수로 한다. 피연산자와 연산자는 실행 사례와 같이 빈칸으로 분리하여 입력한다. 0으로 나누기 시 "0으로 나눌 수 없습니다."를 출력하고 종료한다.

연산>>2+4

2+4의 계산결과는 6

12-1) 연산 식을 구분할 때 if-else문을 이용하여 프로그램을 작성하라.

12-2) 연산 식을 구분할 때 switch문을 이용하여 프로그램을 작성하라.

내풀이

2-9)

import java.util.Scanner;

public class Practice {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		
		System.out.print("원의 중심과 반지름 입력>>");
		int radiusX = scan.nextInt();
		int radiusY = scan.nextInt();
		double radiusR = scan.nextDouble();
		
		System.out.print("점 입력>>");
		double spotX = scan.nextInt();
		double spotY = scan.nextInt();
		
		InCircle.Circle(radiusX,radiusY,spotX, spotY, radiusR);
		System.out.println("점 ("+spotX+", "+spotY +")" + "는 원 안에 있다.");
		
	}

}
class InCircle {
	
//  점은 중심에서 반지름 뺀값과 중심에서 반지름을 더한값 사이에 있으면 원 안에 있는 점
	public static boolean Circle(int x, int y, double spotX, double spotY, double radius) {
		
		if((spotX <=x+radius && spotX>=x-radius)&& (spotY <=y+radius && spotY>=y-radius)) {
		return true;
		}
		else 
			return false;
	}
	
}

2-10)

import java.util.Scanner;

public class Hw02 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		System.out.print("첫번째 원의 중심과 반지름 입력>>");
		int firstCircleX = scan.nextInt();
		int firstCircleY = scan.nextInt();
		int firstCircleR = scan.nextInt();
		
		System.out.print("두번째 원의 중심과 반지름 입력>>");
		int secondCircleX = scan.nextInt();
		int secondCircleY = scan.nextInt();
		int secondCircleR = scan.nextInt();
		
		if(judge(firstCircleX, firstCircleY, firstCircleR, secondCircleX,secondCircleY, secondCircleR))
			System.out.println("두 원은 서로 겹친다.");
		
		
	}

	//두원이 겹치는지 확인하는 메소드
	public static boolean judge(int fx, int fy, int fr, int sx, int sy, int sr) {
		
		//첫번째 원 중심의 값이 두번째 원의 범위에 있는지 확인
		if((fx<=sx+sr && fx>=sx-sr)&&(fy<=sy+sr && fy>=sy-sr))
			return true;
		else 
			return false;
		
	}
}

2-11)

1번

import java.util.Scanner;

public class Hw02 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		System.out.print("달을 입력하세요(1~12)>>");
		int month = scan.nextInt();
		
		if(month>=3 && month<=5) {
			System.out.println("봄");
		}else if(month>=6 && month<=8) {
			System.out.println("여름");
		}else if(month>=9 && month<=11) {
			System.out.println("가을");
		}else if(month==12 || month==1 || month==2) {
			System.out.println("겨울");
		}else
			System.out.println(" 잘못입력");
			
	}
}

2번)

import java.util.Scanner;

public class Hw02 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		System.out.print("달을 입력하세요(1~12)>>");
		int month = scan.nextInt();
		
		
		switch(month) {
		case 3,4,5:  System.out.println("봄"); break;
		case 6,7,8: System.out.println("여름"); break;
		case 9,10,11: System.out.println("가을"); break;
		case 12,1,2: System.out.println("겨울"); break;
		default : System.out.println(" 잘못입력"); break;
		}
	}
}

2-12)

1번)

import java.util.Scanner;

public class Hw02 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		System.out.print("연산>>");
		double number1 = scan.nextDouble();
		String symbol = scan.next();
		
		//scanner에는 char를 받는 함수가 없음
		//첫번째 글자를 저장
		char operations = symbol.charAt(0);
		double number2 = scan.nextDouble();
		
		
		if(operations =='*')
			System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1*number2));
		else if(operations =='/')
			if(number2==0)
				System.out.println("0으로 나눌 수 없습니다.");
			else
			System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1/number2));
		else if(operations =='+')
			System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1+number2));
		else if(operations =='-')
			System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1-number2));

	}
}

2번)

import java.util.Scanner;

public class Hw02 {
	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		System.out.print("연산>>");
		double number1 = scan.nextDouble();
		String symbol = scan.next();
		
		//scanner에는 char를 받는 함수가 없음
		//첫번째 글자를 저장
		char operations = symbol.charAt(0);
		double number2 = scan.nextDouble();
		
		
		switch(operations) {
		case '*': System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1*number2)); break;
		case '/': 
			if(number2==0)
				System.out.println("0으로 나눌 수 없습니다.");
			else
				System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1/number2));
			break;
		case '+': System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1+number2)); break;
		case '-': System.out.println(number1+""+operations+""+number2+"의 계산 결과는 "+(number1-number2)); break;
		default : break;
		
		}
	}
}

728x90