티스토리 뷰

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

 

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


1. 영문 소문자를 하나 입력받고 그 문자보다 알파벳 순위가 낮은 모든 문자를 출력하는 프로그램을 작성하라.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// input your code hereimport java.util.Scanner;
public class One {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        
        int i,j,cnt,n;
        char alphabet;
        char printal;
        
        alphabet = sc.next().charAt(0);
        n = (int)alphabet;
        
        for(i=97;i<=n;i++){
            cnt=i;
            for(j=cnt;j<=n;j++){
                printal = (char)j;
                System.out.print(printal);
            }
            System.out.println();
        }
    }
}
 
cs

 

 


2. 정수를 10개 입력받아 배열에 저장한 후, 배열을 검색하여 3의 배수만 골라 출력하는 프로그램을 작성하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.Scanner;
public class Two {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        
        int[] arr = new int[10];
        
        System.out.print("정수 10개 입력>>");
        for(int i = 0; i<10; i++){
            arr[i] = sc.nextInt();
        }
        
        
        for(int i = 0; i < 10; i++){
            if(arr[i]%3==0)
                System.out.print(arr[i]+" ");
            }
    }
}
 
cs


3. 정수를 입력받아 짝수이면 "짝", 홀수이면 "홀"을 출력하는 프로그램을 작성하라. 사용자가 정수를 입력하지 않는 경우에는 프로그램을 종료하라. 정답을 통해 try catch-finally를 사용하는 정통적인 코드를 볼 수 있다.

 

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;
import java.util.InputMismatchException;
 
public class Three {
    public static void main(String[] argv){
        Scanner sc= new Scanner(System.in); 
        
        System.out.print("정수를 입력하세요. >> "); 
        
        try { 
            int number = sc.nextInt(); 
            
            if ( number % == 
                System.out.println("짝"); 
            
            else if ( number % != 
                System.out.println("홀"); 
            
        }
        
        catch (InputMismatchException e){
            System.out.println("수를 입력하지 않아 프로그램을 종료합니다."); 
        }
    
        finally{  
            sc.close(); 
        } 
    }
}
cs


 4.‘일’, ‘월’, ‘화’, ‘수’, ‘목’, ‘금’, ‘토’ 로 초기화된 문자 배열 day를 선언하고, 사용자로부터 정수를 입력받아 7(배열 day의 크기)로 나눈 나머지를 인덱스로 하여 배열 day에 저장된 문자를 출력하라. 음수가 입력되면 프로그램을 종료하라. 아래 실행결과와 같이 예외 처리하라.

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
import java.util.Scanner;
import java.util.InputMismatchException;
 
public class Four {
    public static void main(String[] argv){
        Scanner sc= new Scanner(System.in); 
        
        char[] arr = {'일','월','화','수','목','금','토'};
        
        while(true){
            System.out.print("정수를 입력하세요>>");
            try{
                int number =sc.nextInt();
                if(number < 0){
                    System.out.println("프로그램 종료.");
                    break;
                }
                
                else{
                    int i = number % 7;
                    System.out.println(arr[i]);
                }
            }
            
            catch(InputMismatchException e){
                System.out.println("수를 입력하세요.");
                break;
            }
        }
        
    }
}
 
cs


5. 정수를 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
import java.util.Scanner;
import java.util.InputMismatchException;
 
public class Five {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        
        int[] arr = new int[10];
        int i, j, temp;
        
        System.out.print("정수 10개 입력>>");
        
        for(i = 0; i < 10; i++)
            arr[i] = sc.nextInt();
        
        for(i = 0; i < 9; i++){
            for(j = i+1; j < 10; j++){
                if(arr[i]>arr[j]){
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        
        for(i = 0; i < 10; i++)
            System.out.print(arr[i] + " ");
    }
}
 
cs

 


6. 다음과 같이 영어와 한글의 짝을 이루는 2개의 배열을 만들고, 사용자로부터 영어 단어를 입력받아 한글을 출력하는 프로그램을 작성하라. “exit”을 입력하면 프로그램을 종료하라.

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
import java.util.Scanner;
import java.util.InputMismatchException;
 
public class Si {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        
        String eng[] = {"student","love","java","happy","future"};
        String kor[] = {"학생","사랑","자바","행복","미래"};
        
        while(true){
            System.out.print("영어 단어 입력>>");
            String s = sc.next();
            
            if(s.equals("exit")){
                System.out.println("프로그램 종료.");
                break;
            }
            
            else{
                int i = 0;
                
                while(true){
                    if(s.equals(eng[i])){
                        System.out.println(kor[i]);
                        break;
                    }
                    else if(!s.equals(eng[i]) && i == 4){
                        System.out.println("그런 단어는 없습니다.");
                        break;
                    }
                    i++;
                }
            }
        }    
    }
}
 
cs


7. 1부터 99까지 369게임에서 박수를 쳐야 하는 경우, 순서대로 화면에 출력하라.

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
import java.util.Scanner;
import java.util.InputMismatchException;
 
public class Seven {
    public static void main(String[] argv){
        Scanner sc = new Scanner(System.in);
        
        int first, second;
        
        for(int i = 1; i <= 99; i++){
            first = i/10;
            second = i%10;
            
            if(first %== && first !=0){
                if(first != && second % != 0)
                    System.out.println(i + " 박수 한 번");
                
                else if(first != && second % 3==0)
                    if(second !=0)
                        System.out.println(i + " 박수 두 번");
                    else
                        System.out.println(i + " 박수 한 번");
                        
            }
            
            else if(second % == && second != 0){
                if(second != 0)
                    System.out.println(i + " 박수 한 번");
                else
                    System.out.println(i + " 박수 없음");
            }
            
            else //if(first % 3 != 0 && second %3 != 0)
                System.out.println(i + " 박수 없음");
        
        }
        
    }
}
 
cs

 


8.퓨터와 사용자의 가위바위보 게임 프로그램을 작성하라. 사용자가 입력하고 <Enter> 키를 치면, 컴퓨터는 랜덤하게 가위, 바위, 보 중 하나를 선택한다. 그리고 누가 이겼는지를 출력한다. ”그만“을 입력하면 게임을 종료한다.

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
import java.util.Scanner
public class Prac { 
   public static void main(String[] argv){ 
      Scanner sc = new Scanner(System.in); 
      String player; 
      String[] computer = {"가위","바위","보"}; 
      int n; 
      System.out.println("가위바위보 하자 "); 
      while(true){ 
         n = ((int)(Math.random()*10))%3;
         System.out.print("내라 >>"); 
         player = sc.next(); 
         
         System.out.println("니 : " + player + " 컴터 : "+ computer[n]); 
         
         if(player.equals("그만")){ 
             System.out.println("프로그램 종료"); 
             System.exit(0);
          } 
         
         else if(player.equals(computer[n])){ 
            System.out.println("비김"); 
            continue
         } 
         
         else if(!player.equals(computer[n])){ 
            if(n==0){ 
               if(player.equals("바위")){ 
                  System.out.println("이김"); 
                  continue
               } 
               else
                  System.out.println("짐"); 
                  continue
               } 
            } 
            
            else if(n==1){ 
                if(player.equals("가위")){ 
                   System.out.println("짐"); 
                   continue
                } 
                else
                   System.out.println("이김"); 
                   continue
                } 
             } 
         } 
      } 
   } 
cs

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



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

 

댓글