JAVA Programming/명품 JAVA 프로그래밍

명품 자바 연습문제 2장 실습 문제

psy_er 2021. 10. 4. 00:52
728x90

명품 자바 연습문제 2장 실습 문제

실습 문제



1. Scanner 클래스를 이용하여 원화를 입력받아 달러로 바꾸어 다음 예시와 같이 출력하는 프로그램을 작성하라. $1 = 1100원으로 가정하고 계산하라.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int won;
		double dollar;
		Scanner s = new Scanner(System.in);
		System.out.print("원화를 입력하세요(단위 원)>>");
		won = s.nextInt();
		dollar = won/1100;
		System.out.println(won+"원은 "+"$"+dollar+"입니다.");
		
	}
}

 

 

 

728x90

 


2. Scanner 클래스를 이용하여 2자리의 정수(10~99 사이)를 입력받고, 십의 자리와 1의 자리가 같은지 판별하여 출력하는 프로그램을 작성하라.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int num;
		int ten;
		int one;
		Scanner s = new Scanner(System.in);
		System.out.print("2자리수 정수 입력(10-99)>>");
		num = s.nextInt();
		ten = num/10;
		one = num%10;

		if(ten == one) {
			System.out.println("Yes! 10의 자리와 1의 자리가 같습니다.");
		}
		else
			System.out.println("No! 10의 자리와 1의 자리가 다릅니다.");
		
	}
}

 

 

 

728x90

 


3. Scanner 클래스를 이용하여 정수로 된 돈의 액수를 입력받아 오만 원권, 만 원권, 천 원권, 500원짜리 동전, 100원짜리 동전, 50원짜리 동전, 10원짜리 동전, 1원짜리 동전 각 몇 개로 변환되는지 출력하라.

 

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int money;
		int C50000=0; int C10000=0;
		int C1000=0; int C500=0;
		int C100=0; int C50=0;
		int C10=0; int C1=0;
		Scanner s = new Scanner(System.in);
		System.out.print("금액을 입력하시오>>");
		money = s.nextInt();
		
		C50000 = money/50000;money = money%50000;
		if(C50000 !=0) 
			System.out.println("오만원권 " + C50000 +"매");
	
		C10000 = money/10000;money = money%10000;
		if(C10000 !=0)
			System.out.println("만원권 " + C10000 +"매");
		
		C1000 = money/1000;money = money%1000;
		if(C1000 !=0)
			System.out.println("천원권 " + C1000 +"매");
		
		C500 = money/500;money = money%500;
		if(C500 !=0)
			System.out.println("오백원 " + C500 +"개");
		
		C100 = money/100;money = money%100;
		if(C100 !=0)
			System.out.println("백원 " + C100 +"개");
		
		C50 = money/50;money = money%50;
		if(C50 !=0)
			System.out.println("오십원 " + C50 +"개");

		C10 = money/10;money = money%10;
		if(C10 !=0)
			System.out.println("십원 " + C10 +"개");
		
		C1 = money/1;money = money%1;
		if(C1 !=0)
			System.out.println("일원 " + C1 +"개");
		

	}
}

 

 

 

728x90

 

 


4. Scanner 클래스로 정수 3개를 입력받고 3개의 숫자 중 중간 크기의 수를 출력하라. 평균값을 구하는 것이 아님에 주의하라.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		
		int A, B, C;
		int Max,Mid,Min;
		Scanner s = new Scanner(System.in);
		System.out.print("정수 3개 입력>>");
		A = s.nextInt();
		B = s.nextInt();
		C = s.nextInt();
		
		if(A>B) {
			Max = A;
			Min = B;
			if(Max>C) {
				if(C>Min) 
					Mid = C;
				else {
					Mid = Min;
					Min = C;
				}
			}
			else {
				Mid = Max;
				Max = C;
			}
		}
		else {
			Max = B;
			Min = A;
			if(Max>C) {
				if(C>Min) 
					Mid = C;
				else {
					Mid = Min;
					Min = C;
				}
			}
			else {
				Mid = Max;
				Max = C;
			}
		}
		System.out.println("중간 값은 " + Mid);
	}
}

 

 

 

728x90

 

 


5. Scanner를 이용하여 삼각형의 변의 길이를 나타내는 정수를 3개 입력받고 이 3개의 수로 삼각형을 만들 수 있는지 판별하라. 삼각형이 되려면 두 변의 합이 다른 한 변의 합보다 커야 한다.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int a,b,c;
		Scanner s = new Scanner(System.in);
		System.out.print("정수 3개를 입력하시오>>");
		a = s.nextInt();
		b = s.nextInt();
		c = s.nextInt();
		if( ((a+b)>c)==true &&((a+c)>b) == true && ((c+b)>a) == true) {
			System.out.println("삼각형이 됩니다.");
		}
		else
			System.out.println("삼각형이 안됩니다.");
	}
}

 

 

 

728x90

 


6. 369게임을 간단히 작성해보자. 1~99까지 정수를 입력받고 정수에 3,6,9 중 하나가 있는 경우는 “박수 짝”을 출력하고 두 개 있는 경우는 “박수 짝짝”을 출력하는 프로그램을 작성하라. 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int a;
		int ten;
		int one;
		Scanner s = new Scanner(System.in);
		System.out.print("1~99 사이의 정수를 입력하시오>>");
		a = s.nextInt();
		
		ten = a/10;
		one = a%10;
		
		if((ten%3)== 0) {
			if((one%3)== 0) {
				System.out.println("박수짝짝");
			}
			else
				System.out.println("박수짝");
		}
		else if((one%3)==0)
			System.out.println("박수짝");

	}
}

 

 

 

728x90

 

 


7. 2차원 평면에서 직사각형은 왼쪽 상단 모서리와 오른쪽 하단 모리의 두 점으로 표현한다. (100,100)과 (200,200)의 두 점으로 이루어진 사각형이 있을 때. Scanner를 이용하여 정수 x와 y값을 입력받고 점 (x, y)가 이 직사각형 안에 있는지를 판별하는 프로그램을 작성하라.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int x;
		int y;
		
		Scanner s = new Scanner(System.in);
		System.out.print("점 (x,y)의 좌표를 입력하시오>>");
		x = s.nextInt();
		y = s.nextInt();
		
		if(100 <= x && x <= 200) {
			if(100 <= y && y<= 200)
				System.out.println("("+x+","+y+")는 사각형 안에 있습니다.");
			else
				System.out.println("("+x+","+y+")는 사각형 안에 없습니다.");
		}
		else
			System.out.println("("+x+","+y+")는 사각형 안에 없습니다.");
	}
}

 

 

 

728x90

 


8. 2차원 평면에서 직사각형은 문제 7번처럼 두 점으로 표현된다. 키보드로부터 직사각형을 구성하는 두 점 (x1, y1), (x2, y2)를 입력받아 (100,100), (200,200)의 두 점으로 이루어진 직사각형과 충돌하는지 판별하는 프로그램을 작성하라.

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int x1; int x2;
		int y1; int y2;
		
		Scanner s = new Scanner(System.in);
		System.out.print("점 (x1,y1), (x2,y2)의 좌표를 입력하시오>>");
		x1 = s.nextInt();
		y1 = s.nextInt();
		x2 = s.nextInt();
		y2 = s.nextInt();
		
		if (inRect(x1,y1,100,100,200,200) || inRect(x2,y2,100,100,200,200) || inRect(x1,y2,100,100,200,200) || inRect(x2,y1,100,100,200,200))
			System.out.println("사각형이 겹칩니다."); 
		else if ((inRect(100,100,x1,y1,x2,y2)) || inRect(100,200,x1,y1,x2,y2) || inRect(200,100,x1,y1,x2,y2) || inRect(200,200,x1,y1,x2,y2)) 
			System.out.println("사각형이 겹칩니다."); 
		else 
			System.out.println("사각형이 겹치지 않습니다.");


	}
	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;
		
	}
}

 

 

 

728x90

 

 


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

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static boolean InnerCircle(double mX, double mY, double r, double x, double y){
		double result;
		result = (mX-x)*(mX-x) + (mY-y)*(mY-y);
		result = Math.sqrt(result);
		if(result <= r)
			return true;
		else
			return false;
	}
	
	public static void main(String[] args) {
		double mX; double mY; double r;
		double x; double y;
		Scanner s = new Scanner(System.in);
		
		System.out.print("원의 중심과 반지름 입력>>");
		mX = s.nextDouble();
		mY = s.nextDouble();
		r = s.nextDouble();
		
		System.out.print("점 입력>>");
		x = s.nextDouble();
		y = s.nextDouble();
		
		if(InnerCircle(mX,mY,r,x,y)) {
			System.out.println("점 ("+ x + ","+y+")는 원 안에 있다.");
		}
	}
}

 

 

 

728x90

 

 


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

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static boolean InnerCircle(double mX1, double mY1, double r1, double mX2, double mY2, double r2){
		double result;
		result = (mX1-mX2)*(mX1-mX2) + (mY1-mY2)*(mY1-mY2);
		result = Math.sqrt(result);
		if(result <= r1+r2)
			return true;
		else
			return false;
	}
	
	public static void main(String[] args) {
		double mX1; double mY1; double r1;
		double mX2; double mY2; double r2;
		Scanner s = new Scanner(System.in);
		
		System.out.print("첫번째 원의 중심과 반지름 입력>>");
		mX1 = s.nextDouble();
		mY1 = s.nextDouble();
		r1 = s.nextDouble();
		
		System.out.print("두번째 원의 중심과 반지름 입력>>");
		mX2 = s.nextDouble();
		mY2 = s.nextDouble();
		r2 = s.nextDouble();
		
		if(InnerCircle(mX1,mY1,r1,mX2,mY2,r2)) {
			System.out.println("두 원은 서로 겹친다.");
		}
		else
			System.out.println("두 원은 서로 겹치지않는다.");
	}
}

 

 

 

728x90

 

 


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


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

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


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

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		int month;
		Scanner s = new Scanner(System.in);
		System.out.print("달을 입력하세요(1~12)>>");
		month = s.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;
		}
	}
}

 

 

 

 

728x90

 

 

 

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

 

package 명품;
import java.util.Scanner;
public class Samplepro {
	public static void main(String[] args) {
		double num1; double result;
		double num2;
		String oper;
		Scanner s = new Scanner(System.in);
		System.out.print("연산>>");
		num1 = s.nextDouble();
		oper = s.next();
		num2 = s.nextDouble();
		
		if (oper == "+") {
			result = num1 + num2;
			System.out.println(num1+oper+num2+"의 계산 결과는 "+result);
		}
		else if(oper == "-") {
			result = num1 - num2;
			System.out.println(num1+oper+num2+"의 계산 결과는 "+result);
		}
		else if(oper == "*") {
			result = num1 * num2;
			System.out.println(num1+oper+num2+"의 계산 결과는 "+result);
		}
		else if(oper == "/") {
			if(num2 == 0) {
				System.out.println("0으로 나눌 수 없습니다.");
			}
			else {
				result = num1 * num2;
				System.out.println(num1+oper+num2+"의 계산 결과는 "+result);
			}
		}
	}
}
728x90