본문 바로가기
IT/프로그래밍

명품 JAVA Programming 2장 실습문제 정답

by nutrient 2020. 12. 12.
728x90
728x170

명품 JAVA Programming 2장 실습문제 정답

 

명품 JAVA Programming 2장 실습문제 정답

 

1번

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

2번

import java.util.Scanner; 
public class Two { 
public static void main(String[] args) { 
System.out.print("2자리수 정수 입력(10~99)>>"); 
Scanner scanner = new Scanner(System.in); 
int num = scanner.nextInt(); 
if (num / 10 == num % 10) { 
System.out.println("Yes! 10의 자리와 1의 자리가 같습니다."); 
} 
else { 
System.out.println("No! 10의 자리와 1의 자리가 다릅니다."); 
} 
scanner.close(); 
} 
}

3번

import java.util.Scanner;

public class Three {

	final static int 오만원 = 50000;
	final static int 만원 = 10000;
	final static int 천원 = 1000;
	final static int 오백원 = 500;
	final static int 백원 = 100;
	final static int 오십원 = 50;
	final static int 십원 = 10;
	final static int 일원 = 1;
	public static void main (String args[])  {
		int res;
		int money;
		Scanner sin = new Scanner(System.in);
		System.out.print("금액을 입력하시오>>");
		money = sin.nextInt();
		res = money/오만원;
		money = money%오만원;
		if(res > 0) {
			System.out.println("오만원권 "+ res + "매");
		}
		res = money/만원;
		money = money%만원;
		if(res > 0) {
			System.out.println("만원권 "+ res + "매");
		}
		res = money/천원;
		money = money%천원;
		if(res > 0) {
			System.out.println("천원권 "+ res + "매");
		}
		res = money/오백원;
		money = money%오백원;
		if(res > 0) {
			System.out.println("오백원 "+ res + "개");
		} 
		res = money/백원;
		money = money%백원;		
		if(res > 0) {
			System.out.println("백원 "+ res + "개");
		}
		res = money/오십원;
		money = money%오십원;
		if(res > 0) {
			System.out.println("오십원 "+ res + "개");
		}
		res = money/십원;
		money = money%십원;
		if(res > 0) {
			System.out.println("십원 "+ res + "개");
		}
		res = money/일원;
		money = money%일원;	
		if(res > 0) {
			System.out.println("일원 "+ res + "개");
		}
		
		sin.close();
	}
}

4번

import java.util.Scanner;

public class Four {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("정수 3개 입력>>");
		int num1 = scanner.nextInt();
		int num2 = scanner.nextInt();
		int num3 = scanner.nextInt();

		System.out.println("중간 값은 " + num2);

		scanner.close();
	}

}

5번

import java.util.Scanner;

public class Five {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		
		System.out.print("정수 3개를 입력하시오>>");
		int n1 = scanner.nextInt();
		int n2 = scanner.nextInt();
		int n3 = scanner.nextInt();
		
		if(((n1+n2)>n3)||((n1+n3)>n2)||(n2+n3)>n1) {
			System.out.println("삼각형이 됩니다");
		}
		else {
			System.out.println("삼각형이 안됩니다");
		}
		
		scanner.close();
	}

}

6번

import java.util.Scanner;

public class Six {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("1~99 사이의 정수를 입력하시오>>");
		int i = scanner.nextInt();

		if (i % 10 == 3 || i % 10 == 3 || i % 10 == 3) {
			System.out.println("박수짝");
		} else if ((i % 10 == 3 || i % 10 == 6 || i % 10 == 9) && (i / 10 == 3 || i / 10 == 6 || i / 10 == 9)) {
			System.out.println("박수짝짝");
		}

		scanner.close();
	}

}

7번

import java.util.Scanner;

public class Seven {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		System.out.print("점 (x,y)의 좌표를 입력하시오>>");
		int x = scanner.nextInt();
		int y = scanner.nextInt();

		if ((x >= 100 && x <= 200) && (y >= 100 && y <= 200)) {
			System.out.println("(" + x + ", " + y + ")는 사각형 안에 있습니다.");
		} else {
			System.out.println("(" + x + ", " + y + ")는 사각형 안에 없습니다.");
		}

		scanner.close();
	}

}

8번

import java.util.Scanner;

public class Eight {

	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("첫번째 점(x1, y1)을 입력하시오>>");
		int x1 = scanner.nextInt();
		int y1 = scanner.nextInt();
		System.out.print("두번째 점(x2, y2)을 입력하시오>>");
		int x2 = scanner.nextInt();
		int y2 = scanner.nextInt();

		scanner.close();
	}

}

9번

import java.util.Scanner;

public class CircleInclusion {

 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("원의 중심과 반지름 입력>>");
  double x = scanner.nextDouble();
  double y = scanner.nextDouble();
  double radius = scanner.nextDouble();
  
  System.out.print("점 입력>>");
  double x2 = scanner.nextDouble();
  double y2 = scanner.nextDouble();
  
  double distance = Math.sqrt((x-x2)*(x-x2) + (y-y2)*(y-y2));
  if(distance < radius)
   System.out.print("점 (" + x2 + ", " + y2 + ")는 원 안에 있다.");
  else
   System.out.print("점 (" + x2 + "," + y2 + ")는 원 밖에 있다.");

  scanner.close();
 }

}

10번

import java.util.Scanner;

public class Ten {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.print("첫번째 원의 중심과 반지름 입력>>");
		int x1 = sc.nextInt();
		int y1 = sc.nextInt();
		int r1 = sc.nextInt();
		System.out.print("두번째 원의 중심과 반지름 입력>>");
		int x2 = sc.nextInt();
		int y2 = sc.nextInt();
		int r2 = sc.nextInt();

		if ((x1 >= 50 && x1 <= 100) && (y1 >= 50 && y1 <= 100)) {
			System.out.println("두 원은 서로 겹친다.");
		} else if ((x2 >= 50 && x2 <= 100) && (y2 >= 50 && y2 <= 100)) {
			System.out.println("두 원은 서로 겹친다.");
		} else {
			System.out.println("두 원은 서로 안겹친다.");
		}

		sc.close();
	}

}

11번

import java.util.Scanner;

public class Eleven {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		System.out.print("달을 입력하세요(1~12)>>");
		int month = sc.nextInt();

		switch (month) {
		case 12:
		case 1:
		case 2:
			System.out.println("겨울");
			break;
		case 3:
		case 4:
		case 5:
			System.out.println("봄");
			break;
		case 6:
		case 7:
		case 8:
			System.out.println("여름");
			break;
		case 9:
		case 10:
		case 11:
			System.out.println("가을");
			break;

		default:
			System.out.println("바르게 입력해라.");
			break;
		}

		sc.close();
	}

}

12번

import java.util.Scanner;

public class ArithmeticExIf {

 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("연산>>");
  int op1 = scanner.nextInt();
  String op = scanner.next();  
  int op2 = scanner.nextInt();
  
  int res = 0;
  if(op.equals("+")) 
   res = op1 + op2;
  else if(op.equals("-")) 
   res = op1 - op2;
  else if(op.equals("*")) 
   res = op1 * op2;
  else if(op.equals("/")) {
   if(op2 == 0) { 
    System.out.print("0으로 나눌 수 없습니다.");
    scanner.close();
    return;
   }
   else
    res = op1 / op2;
  }
  else {
   System.out.print("사칙연산이 아닙니다.");
   scanner.close();
   return;
  }
  System.out.print(op1 + op + op2 + "의 계산 결과는 " + res);
  scanner.close();
 }

}


13번

import java.util.Scanner;

public class ArithmeticExSwitch {

 public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("연산>>");
  int op1 = scanner.nextInt();
  String op = scanner.next();  
  int op2 = scanner.nextInt();
  
  int res = 0;
  switch(op) {
   case "+":
    res = op1 + op2;
    break;
   case "-":
    res = op1 - op2;
    break;
   case "*":
    res = op1 * op2;
    break;  
   case "/":
    if(op2 == 0) {
     System.out.print("0으로 나눌 수 없습니다.");
     scanner.close();
     return;
    }
    res = op1 / op2;
    break;
   default :
    System.out.print("사칙연산이 아닙니다.");
    scanner.close();
    return;
  }
  System.out.print(op1 + op + op2 + "의 계산 결과는 " + res);
  scanner.close();
 }
}


 

728x90
그리드형

댓글