본문 바로가기

[책]명품 JAVA Programming

명품 자바 프로그래밍 2장 Open Challenge& 실습문제

Open Challenge

가위바위보 게임입니다. 가위, 바위, 보 중에서 입력하세요.

철수 >>

영희 >>

??가 이겼습니다.

 

답-

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package OpenChallenge;
import java.util.Scanner;
 
public class OpenChallenge {
 
    public static void main(String[] args) {
        System.out.println("가위 바위 보 게임입니다. 가위, 바위, 보 중에서 입력하세요");
        Scanner sc = new Scanner(System.in);
        
        //철수 입력 받을거. println하면 다음줄로 넘어가니 그냥 print해야함.
        System.out.print("철수 >> ");
        String user1 = sc.next();  // 띄어쓰기 안하는 한단어만 받을거니까 nextLine안함.
        
        //영희 입력 받을거.
        System.out.print("영희 >> ");
        String user2 = sc.next();
        
        //switch든 if든 상관없는데 if가 쉬워보이니 if로 해보겠습니다.
        if(user1.equals("가위")) {
            if(user2.contentEquals("가위")) {
                System.out.println("비겼습니다.");
            }else if(user2.contentEquals("바위")) {
                System.out.println("영희가 이겼습니다.");
            }else if(user2.contentEquals("보")) {
                System.out.println("철수가 이겼습니다.");
            }else {
                System.out.println("가위 바위 보 중에 입력하세요.");
            }
        }
        else if(user1.equals("바위")) {
            if(user2.contentEquals("가위")) {
                System.out.println("철수가 이겼습니다");
            }else if(user2.contentEquals("바위")) {
                System.out.println("비겼습니다");-
            }else if(user2.contentEquals("보")) {
                System.out.println("영희가 이겼습니다.");
            }else {
                System.out.println("가위 바위 보 중에 입력하세요.");
            }
        }
        else if(user1.equals("보")) {
            if(user2.contentEquals("가위")) {
                System.out.println("영희가 이겼습니다.");
            }else if(user2.contentEquals("바위")) {
                System.out.println("철수가 이겼습니다.");
            }else if(user2.contentEquals("보")) {
                System.out.println("비겼습니다.");
            }else {
                System.out.println("가위 바위 보 중에 입력하세요.");
            }
        }
        else {
            System.out.println("가위 바위 보 중에 입력하세요.");
        }
 
    }
}
 
cs

 

if문을 중첩해서 사용해서 풀어야 되는 문제였다.

 

 

 

실습문제

 

1. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package RealQuetion;
import java.util.Scanner;
 
public class Q1 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.print("원화를 입력하세요(단위 원)>> ");
        long a=sc.nextInt();
        
        double b=a/1100;
        System.out.println(a+"원은 $"+b+"입니다.");
        
        sc.close();
 
    }
 
}
 
cs

 

2.

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

3.

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
package RealQuetion;
import java.util.Scanner;
 
public class Q3 {
 
    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            
            System.out.print("금액을 입력하시오>> ");
            int a=sc.nextInt();
            
            System.out.println("5만원권: "+a/50000);
            a%=50000;
            System.out.println("1만원권: "+a/10000);
            a%=10000;
            System.out.println("5천원권: "+a/5000);
            a%=5000;
            System.out.println("1천원권: "+a/1000);
            a%=1000;
            System.out.println("5백원권: "+a/500);
            a%=500;
            System.out.println("1백원권: "+a/100);
            a%=100;
            System.out.println("오십원권: "+a/50);
            a%=50;
            System.out.println("십원권: "+a/10);
            a%=10;
            System.out.println("일원권: "+a);
                    
            sc.close();
    }
 
}
 
cs

4.

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
package RealQuetion;
import java.util.Scanner;
 
public class Q4 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
    
        System.out.print("정수 3개 입력하세요>>");
        int a=sc.nextInt();
        int b=sc.nextInt();
        int c=sc.nextInt();
        
        if(a<b&&b<c)
            System.out.println("중간값은 "+ b+" 입니다.");
        else if(a>b&&b>c)
            System.out.println("중간값은 "+ b+" 입니다.");
        else if(b<a&&a<c)
            System.out.println("중간값은 "+ a+" 입니다.");
        else if(b>a&&a>c)
            System.out.println("중간값은 "+ a+" 입니다.");
        else
            System.out.println("중간값은 "+ c+" 입니다.");
        
        sc.close();
    
    
    }
}
 
cs

5.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package RealQuetion;
import java.util.Scanner;
 
public class Q5 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.print("정수 3개를 입력하시오>> ");
        int a=sc.nextInt();
        int b=sc.nextInt();
        int c=sc.nextInt();
        //삼각형의 특징= 양변의 길이의 합은 무조건 다른 한변보다 길어야 한다. 이걸 구현한것이 아래 if의 조건문
        if(a+b>=c&&a+c>=b&&b+c>=a)
            System.out.println("삼각형이 됩니다.");
        else
            System.out.println("NO");
        sc.close();
    }
 
}
 
cs

6.

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
package RealQuetion;
import java.util.Scanner;
 
public class Q6 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int a=0;
        
        System.out.print("1~99사이의 정수를 입력하시오>>");
        a=sc.nextInt();
        
        if(a>=100) {
            System.out.println("1~99사이의 정수만 입력해주시요.");
            System.exit(0);   //반복문을 통해 만약에 100이상이면 다시 입력하도록 하는 방법이 있는데 반복을 안배웠으니 그냥 프로그램 종료 하게 만듬.
        }
        
        int x=a/10;
        int y=a%10;
        if((x==3||x==6||x==9)&&(y==3|y==6|y==9))
          System.out.println("박수 짝짝");
        else if((x==3||x==6||x==9)||(y==3|y==6|y==9))
            System.out.println("박수 짝");
        else
            System.out.println(a+" 쿵쿵따~");
        
        sc.close();
            
 
    }
 
}
 
cs

7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package RealQuetion;
import java.util.Scanner;
 
public class Q7 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.print("점 (x,y)의 좌표를 입력하시오>>");
        int x1=sc.nextInt();
        int y1=sc.nextInt();
        
        if(x1<200&&x1>100&&y1>100&&y1<200)
            System.out.print("[100,100],[200,200] 사각형 안에 있습니다.");
        else
            System.out.print("사각형 밖입니다!");
        
        sc.close();
    }
 
}
cs

8.

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
package RealQuetion;
import java.util.Scanner;
 
public class Q8 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("점 x1, y1을 입력하세요(공백으로 인식): ");
        int x1 = sc.nextInt();
        int y1 = sc.nextInt();
        
        System.out.print("점 x2, y2를 입력하세요(공백으로 인식): ");
        int x2 = sc.nextInt();
        int y2 = sc.nextInt();
        
        
        // 사실 책에 있는 함수를 이용하면 훨씬 쉽게 충돌 판단을 할 수 있다.
        // 하지만 우리는 아직 함수가 뭔지 안배웠으니까 노가다로 합니다.
        // x1,y1 을 우리가 만드는 직사각형의 왼쪽아래점 x2,y2를 오른쪽 위의 점으로 직사각형 그리는것.
        // 충돌판정은 중요하므로 꼭 직접 생각해 보고 조건문 안이 왜 그렇게 됐는지 이해 안되면 손으로 써보세요!
        if ( ( (x1 >=100 && x1 <=200&& (y1 >=100 && y1 <= 200) ) ||
                ((x2 >=100 && x2 <=200&& (y2 >= 100 && y2 <= 200) ) ) {
            System.out.println("충돌 했습니다.");
        }
        else {
            System.out.println("충돌 안했네요.");
        }
        
        sc.close();
    }
 
}
 
cs

9.

최대한 배운 내용만 사용하고 싶었는데, 수학 공식상 루트를 사용 했습니다. 물론 그냥 제곱으로 처리해도 되지만 정확성을 위해 이렇게 처리 했습니다.

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
package RealQuetion;
import java.util.Scanner;
 
public class Q9 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        System.out.print("원의 중심과 반지름 입력>> ");
        int a=sc.nextInt();
        int b=sc.nextInt();
        double r=sc.nextDouble();        
        
        System.out.print("점 입력>> ");
        double x=sc.nextDouble();
        double y=sc.nextDouble();
        
        // 점이 원 내부에 있는지는 입력받은 두 점과 원의 중심까지의 거리가 r보다 작으면 됩니다.
        double result =Math.sqrt((x-a)*(x-a)+(y-b)*(y-b));    //Math.sqrt()는 괄호안의 내용을 루트 씌워주는 함수 입니다!
        
        if(result<=r)
            System.out.println("점 ("+x+","+y+")는 원 안에 있다.");
        else
            System.out.println("점 ("+x+","+y+")는 원 밖에 있다.");
        
        sc.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
25
26
27
28
29
30
31
package RealQuetion;
import java.util.Scanner;
 
public class Q10 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("첫번째 원의 중심과 반지름 입력>>");
        int x1 = sc.nextInt();
        int y1 = sc.nextInt();
        double r1 = sc.nextDouble();
        
        System.out.print("두번째 원의 중심과 반지름 입력>>");
        int x2 = sc.nextInt();
        int y2 = sc.nextInt();
        double r2 = sc.nextDouble();
        
        //이제 조건을 완성해야 하는데, 고등학교 수학시간에 두 원이 겹치는지 확인하는 조건식을 배웠을 것이다.
        //간단하게 두 원의 거리 d 가 r1+r2보다 작거나 같으면 된다.
        double d = Math.sqrt( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
        if( d<= r1+r2) {
            System.out.println("두 원은 서로 겹친다.");
        }else {
            System.out.println("두 원은 겹치지 않습니다.");
        }
        sc.close();
    }
 
}
 
cs

11.

여태 if문만 했으니 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
package RealQuetion;
import java.util.Scanner;
 
public class Q11 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("달을 입력하세요(1~12)>>");
        int month = sc.nextInt();
        
        switch(month) {
        case 3case 4case 5:
            System.out.println("봄");
            break;
        case 6case 7case 8:
            System.out.println("여름");
            break;
        case 9case 10case 11:
            System.out.println("가을");
            break;
        default:
            System.out.println("겨울");
        }
    }
 
}
 
cs

12.

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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.Scanner;
 
public class Q12 {
    public static void main(String[] aaaa) {
        System.out.print("연산>>");
        Scanner sc = new Scanner(System.in);
        
        //띄어쓰기로 숫자, 연산자, 숫자 이순서로 받아야 하기 때문에
        int num1 = sc.nextInt();
        String sign = sc.next();
        int num2 = sc.nextInt();
        
        double result = 0//연산 결과로 사용
        
        if (sign.equals("+")) {
            result = num1 + num2;
        }else if(sign.equals("-")) {
            result = num1 - num2;
        }else if (sign.equals("*")) {
            result = num1 * num2;
        }else if (sign.equals("/")) {
            if (num1 == 0) {
                System.out.println("0 으로는 나눌 수 없습니다.");
            }
            else {
                result = (double)num1/(double)num2;
            }
            
        }
        
        //switch문으로 하면 이렇게
        switch(sign) {
            case "+":
                result = num1 + num2;
                break;
            case "-":
                result = num1 - num2;
                break;
            case "*":
                result = num1 * num2;
                break;
            case "/":
                if (num1 == 0) {
                    System.out.println("0 으로는 나눌 수 없습니다.");
                }
                else {
                    result = (double)num1/(double)num2;
                }
                break;
            default:
                System.out.println("+, -, *, / 중에 입력해주세요.");
        }
        
        System.out.println(num1 + "+" + num2 +"의 계산 결과는 " + result);
    }
}
 
cs