티스토리 뷰

명품 자바 에센셜 Chapter 2 실습문제.

 

*진도 순서에 맞는 문법으로만 코딩한 것이며, 아직 나오지 않은 문법은 배제하고 풀이하였습니다.


 

1. 두 정수를 입력받아 합을 구하여 출력하는 프로그램을 작성하라. 키보드 입력은 Scanner클래스를 이용하라.

 

1
2
3
4
5
6
7
8
9
10
11
12
import java.util.Scanner;
public class One {
    public static void main(String[] argv){
    Scanner sc = new Scanner(System.in);
    
    int a = sc.nextInt();
    int b = sc.nextInt();
    
    System.out.println(a+" + "+b+"은 " +(a+b));
    }
}
 
cs

 

 

 


2. 2차원 평면에서 하나의 직사각형은 두 점으로 표현된다. (50, 50)과 (100, 100)의 두 점으로 이루어진 삼각형이 있다고 하자. 한 점을 구성하는 정수 x와 y값을 입력받고 점(x, y)가 이 직사각형 안에 있는지를 판별하는 프로그램을 작성하라

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.Scanner;
public class Two {
    public static void main(String[] argv){
    Scanner sc = new Scanner(System.in);
    int x,y;
    
    System.out.print("점 (x, y)의 좌표를 입력하세요>>");
    x = sc.nextInt(); y = sc.nextInt();
    
    if(x>=50 && x<=100 && y>=50 && y<=100)
        System.out.println("점 ("+ x + ","+ y +")은 (50, 50)과 (100, 100)의 사각형 내에 있습니다.");
    
    else
        System.out.println("사각형 내에 없습니다.");
    }
}
 
cs

 


3. 다음과 같이 AND와 OR의 논리 연산을 입력받아 결과를 출력하는 프로그램을 작성하라. 예를 들어 'true AND false'의 결과로 false를, 'true OR false'의 결과로 true를 출력하면 된다. 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
import java.util.Scanner;
public class Three {
    public static void main(String[] argv){
        boolean a,b;
        String op;
        Scanner sc = new Scanner(System.in);
        
        System.out.print("논리 연산을 입력하세요>>");
        a = sc.nextBoolean(); op = sc.next(); b = sc.nextBoolean();
        
        switch(op){
        
        case "AND":
            System.out.println(a&b);
            break;
            
        case "OR":
            System.out.println(a|b);
            break;
        }
    }
}
 
cs

 

 


 

4. 돈의 액수를 입력받아 오만원권, 만원권, 천원권, 500원짜리 동전, 100원짜리 동전, 10원짜리 동전, 1원짜리 동전 각 몇 개로 변환되는지 출력하라. 문제 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
34
35
36
37
38
39
import java.util.Scanner;
public class Four {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        int money;
        int number;
        
        System.out.print("돈의 액수를 입력하시오>>");
        money = sc.nextInt();
        
        number = money/50000;
        System.out.print("오만원권"+ number +"개,");
        money %= 50000;
        
        number = money/10000;
        System.out.print("만원권"+ number +"개,");
        money %= 10000;
        
        number = money/1000;
        System.out.print("천원권"+ number +"개,");
        money %= 1000;
        
        number = money/500;
        System.out.print("500원권"+ number +"개,");
        money %= 500;
        
        number = money/100;
        System.out.print("100원권"+ number +"개,");
        money %= 100;
        
        number = money/10;
        System.out.print("10원권"+ number +"개,");
        money %= 10;
        
        number = money/1;
        System.out.println("1원권"+ number +"개");
    }
}
 
cs

 

 


5. 학점이 A, B이면, "Excellnet". 학점이 C, D이면 "Good", 학점이 F이면 "Bye"라고 출력하는 프로그램을 작성하라. switch와 break를 활용하라.

 

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 Five {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        String grade;
        
        System.out.print("학점을 입력하세요>>");
        grade = sc.next();
        
        switch(grade){
        case "A"case "B" :
            System.out.println("Excellent");
            break;
        
        case "C"case "D" :
            System.out.println("Good");
            break;
            
        case "F":
            System.out.println("Bye");
            break;
            
        default :
            System.out.println("잘못 입력");
            
        }
    }
}
 
cs

 

 


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

 

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
import java.util.Scanner;
public class Prac {
    public static void main(String[] argv){
        int number; 
        
        Scanner sc = new Scanner(System.in);
        
        System.out.print("1~99까지의 정수를 입력하세요>>");
        number = sc.nextInt();
        
        int first = number/10;
        int second = number%10;
        
        System.out.print("박수");
        if(first%== || second%== 0){
            if(first % == && first != 0){
                System.out.print("짝");
                
                if(second % == && second != 0)
                    System.out.print("짝");
            }
            
            else if(second % == && second != 0)
                System.out.print("짝");
            
            else 
                System.out.println("없음");
        }
    }
}
 
cs

 

 

참고 문헌 : 명품 자바 에센셜 생능출판 / 황기태



본 게시물은 개인적인 용도로 작성된 게시물입니다. 이후 포트폴리오로 사용될 정리 자료이니 불펌과 무단도용은 하지 말아주시고 개인 공부 목적으로만 이용해주시기 바랍니다.

 

댓글