본문 바로가기

Java/명품 JAVA Programming

명품 JAVA Programming 2장 실습문제

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("원화를 입력하세요(단위 원)>>");
        int won = scanner.nextInt();
        double dollar = won / 1100.0;
        System.out.println(won + "원은 $" + dollar + "입니다.");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("2자리수 정수 입력(10~99)>>");
        int a = scanner.nextInt();
        if (a / 10 == a % 10)
            System.out.print("Yes! 10의 자리와 1의 자리가 같습니다.");
        else
            System.out.print("No! 10의 자리와 1의 자리가 같지 않습니다.");
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("금액을 입력하시오>>");
        int a = scanner.nextInt();
        System.out.println("오만원권 " + a / 50000 + "매");
        a = a % 50000;
        System.out.println("만원권 " + a / 10000 + "매");
        a = a % 10000;
        System.out.println("천원권 " + a / 1000 + "매");
        a = a % 1000;
        System.out.println("백원 " + a / 100 + "개");
        a = a % 100;
        System.out.println("오십원 " + a / 50 + "개");
        a = a % 50;
        System.out.println("십원 " + a / 10 + "개");
        a = a % 10;
        System.out.println("일원 " + a + "개");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int middle = 0;
 
        System.out.print("정수 3개 입력>>");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        if ((c > a && a > b) || (c < a && a < b))
            middle = a;
        else if ((a > b && b > c) || (a < b && b < c))
            middle = b;
        else
            middle = c;
        System.out.println("중간 값은 " + middle);
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        System.out.print("정수 3개를 입력하시오>>");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        if ((a + b > c) && (b + c > a) && (c + a > b))
            System.out.println("삼각형이 됩니다");
        else
            System.out.println("삼각형이 되지 않습니다");
 
        scanner.close();
    }
}
cs

 

 

 

6. 369게임을 간단히 작성해보자. 1~99까지의 정수를 입력받고 정수에 3, 6, 9 중 하나가 있는 경우는 "박수짝"을 출력하고 두 개 있는 경우는 "박수짝짝"을 출력하는 프로그램을 작성하라. 예를 들면, 키보드로 입력된 수가 13인 경우 "박수짝"을, 36인 경우 "박수짝짝"을 출력하면 된다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = 0;
 
        System.out.print("1~99 사이의 정수를 입력하시오>>");
        int a = scanner.nextInt();
        if (a / 10 == 3 || a / 10 == 6 || a / 10 == 9)
            count++;
        if (a % 10 == 3 || a % 10 == 6 || a % 10 == 9)
            count++;
        if (count == 1)
            System.out.println("박수짝");
        else if (count == 2)
            System.out.println("박수짝짝");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.Scanner;
 
public class Main {
    public static boolean inRect(int x, int y, int rectX1, int rectY1,
                                 int rectX2, int rectY2) {
        return (x >= rectX1 && x <= rectX2) && (y >= rectY1 && y <= rectY2);
    }
 
    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 (inRect(x, y, 100100200200))
            System.out.println("(" + x + "," + y + ")는 사각형 안에 있습니다.");
        else
            System.out.println("(" + x + "," + y + ")는 사각형 안에 있지 않습니다.");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Scanner;
 
public class Main {
    public static boolean inRect(int x, int y, int rectX1, int rectY1,
                                 int rectX2, int rectY2) {
        return (x >= rectX1 && x <= rectX2) && (y >= rectY1 && y <= rectY2);
    }
 
    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();
        if (inRect(x1, y1, 100100200200|| inRect(x2, y2, 100100200200))
            System.out.println("직사각형과 충돌합니다.");
        else
            System.out.println("직사각형과 충돌하지 않습니다.");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double distance = 0.0;
 
        System.out.print("원의 중심과 반지름 입력>>");
        double cX = scanner.nextDouble();
        double cY = scanner.nextDouble();
        double cR = scanner.nextDouble();
        System.out.print("점 입력>>");
        double x = scanner.nextDouble();
        double y = scanner.nextDouble();
        distance = Math.sqrt(Math.pow(cX - x, 2+ Math.pow(cY - y, 2));
        if (distance <= cR)
            System.out.println("점 (" + x + ", " + y + ")는 원 안에 있다.");
        else
            System.out.println("점 (" + x + ", " + y + ")는 원 안에 있지 않다.");
 
        scanner.close();
    }
}
cs

 

 

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double distance = 0.0;
 
        System.out.print("첫번째 원의 중심과 반지름 입력>>");
        double x1 = scanner.nextDouble();
        double y1 = scanner.nextDouble();
        double r1 = scanner.nextDouble();
        System.out.print("두번째 원의 중심과 반지름 입력>>");
        double x2 = scanner.nextDouble();
        double y2 = scanner.nextDouble();
        double r2 = scanner.nextDouble();
        distance = Math.sqrt(Math.pow(x1 - x2, 2+ Math.pow(y1 - y2, 2));
        if (distance <= r1 + r2)
            System.out.println("두 원은 서로 겹친다.");
        else
            System.out.println("두 원은 서로 겹치지 않는다.");
 
        scanner.close();
    }
}
cs

 

 

 

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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String season = "";
 
        System.out.print("달을 입력하세요(1~12)>>");
        int month = scanner.nextInt();
        if (month >= 3 && month <= 5)
            season = "봄";
        else if (month >= 6 && month <= 8)
            season = "여름";
        else if (month >= 9 && month <= 11)
            season = "가을";
        else if (month == 12 || month == 1 || month == 2)
            season = "겨울";
        else
            season = "잘못입력";
        System.out.println(season);
 
        scanner.close();
    }
}
cs

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.Scanner;
 
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String season = "";
 
        System.out.print("달을 입력하세요(1~12)>>");
        int month = scanner.nextInt();
        switch (month) {
            case 3:
            case 4:
            case 5:
                season = "봄";
                break;
            case 6:
            case 7:
            case 8:
                season = "여름";
                break;
            case 9:
            case 10:
            case 11:
                season = "가을";
                break;
            case 12:
            case 1:
            case 2:
                season = "겨울";
                break;
            default:
                season = "잘못입력";
                break;
        }
        System.out.println(season);
 
        scanner.close();
    }
}
cs

 

 

 

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

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double result = 0.0;
 
        System.out.print("연산>>");
        double a = scanner.nextDouble();
        String s = scanner.next();
        double b = scanner.nextDouble();
        if (s.equals("+"))
            result = a + b;
        else if (s.equals("-"))
            result = a - b;
        else if (s.equals("*"))
            result = a * b;
        else {
            if (b == 0) {
                System.out.println("0으로 나눌 수 없습니다.");
                return;
            }
            result = a / b;
        }
        System.out.println(a + s + b + "의 계산결과는 " + result);
 
        scanner.close();
    }
}
cs

 

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

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import java.util.Scanner;
 
public class Main2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double result = 0.0;
 
        System.out.print("연산>>");
        double a = scanner.nextDouble();
        String s = scanner.next();
        double b = scanner.nextDouble();
        switch (s) {
            case "+":
                result = a + b;
                break;
            case "-":
                result = a - b;
                break;
            case "*":
                result = a * b;
                break;
            default:
                if (b == 0) {
                    System.out.println("0으로 나눌 수 없습니다.");
                    return;
                }
                result = a / b;
                break;
        }
        System.out.println(a + s + b + "의 계산결과는 " + result);
 
        scanner.close();
    }
}
cs