본문 바로가기

[책]명품 JAVA Programming

명품 자바 프로그래밍 3장 OpenChallenge&실습문제

OpenChallenge

숨겨진 카드의 수를 맞추는 게임을 만들자. 0~99사이의 숫자를 찾는 게임. 입력 숫자보다 높으면 '더 높게' 를 출력하고 낮으면 '더 낮게'를 출력한다. 다 맞추면 재시작 할 수 있게끔 짜야 한다.

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
import java.util.Scanner;
 
public class OpenChallenge {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        while(true) {
            System.out.println("수를 결정하였습니다. 맞추어 보세요\n0~99");
            int i = 1;
            int num = (int)(Math.random()*100);    //0~99숫자 생성.
            System.out.println(num);
            //y일시 반복문을 다시 해야 하므로 이를이렇게 짯음. boolean flag를 true대신 넣어 n일경우 flag를 false로 하는 방법도 있다.
            while(true) {
                System.out.print(i+ ">>");
                int user = sc.nextInt();
                
                if(user > num) {
                    System.out.println("더 낮게.");
                }
                else if(user < num) {
                    System.out.println("더 높게.");
                }else {
                    System.out.println("맞았습니다.");
                    System.out.println("다시하시겠습니까(y/n)>>");
                    String ans = sc.next();
                    if(ans.contentEquals("y")) {
                        break;
                    }else if(ans.contentEquals("n")) {
                        System.exit(0);        //n이면 프로그램 종료!
                    }
                }
            }
            
        }
        
 
    }
 
}
 
cs

 

실습문제)))

1. 다음 프로그램에 대해 물음에 답하라?

(1) sum에 i만큼 더하고 i를 2만큼씩 증가시킨다.

(2) main에서 완성시켜라. 클래스명은 편의상 Q1로 했다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 
public class Q1 {
 
    public static void main(String[] args) {
        int sum = 0, i = 0;
        while(i < 100) {
            sum += i;
            i +=2;
        }
        System.out.println(sum);
    }
 
}
 
cs

 

 

(3)for 문을 이용해 동일하게 실행되게 하라.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
public class Q1 {
 
    public static void main(String[] args) {
        int sum = 0, i;
        for(i = 0; i< 100;i+=2) {
            sum += i;
        }
        System.out.println(sum);
    }
 
}
 
cs

(4)do-while문 이용.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
public class Q1 {
 
    public static void main(String[] args) {
        int sum = 0, i=0;
        do {
            sum += i;
            i+=2;
        }while(i<100);
        
        System.out.println(sum);
    }
 
}
 
cs

 

2. 2차원 배열을 출력하는 프로그램을 작성.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public class Q2 {
 
    public static void main(String[] args) {
        int n[][]= {{1},{1,2,3},{1},
                {1,2,3,4},{1,2}};
        
        for(int i=0;i<n.length;i++) {
            for(int j=0;j<n[i].length;j++)
                System.out.print(n[i][j]+" ");
        System.out.println();
        }
        
 
    }
 
}
cs

3.

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 Q3 {
 
    static public void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("정수를 입력하시오>>");
        int n=sc.nextInt();
        int t=n;
        for(int i=0;i<n;i++) {
            for(int j=0;j<t;j++) {
                System.out.print("*");
                
            }
            t--;
                System.out.println();
                
    }
        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
import java.util.Scanner;
 
public class Q4 {
 
       public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          
          System.out.print("소문자 알파벳 하나를 입력하시오 >> ");
          String s = sc.next(); 
          char c = s.charAt(0); 
          
          for(int i = 0; i<=c-'a'; i++) {
             for(char j = 'a'; j<= c-i; j++) {
                System.out.print(j);
             }
             System.out.println();
          }
          
          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
23
24
import java.util.Scanner;
 
public class Q5 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        int myArr[];
        myArr=new int[10];
        System.out.println("양의 정수 10개를 입력하시오>>");
        for(int i=0;i<myArr.length;i++) {
            myArr[i]=sc.nextInt();
        }
        System.out.print("3의 배수는: ");
        for(int j=0;j<myArr.length;j++) {
            if(myArr[j]%3==0)
                System.out.print(myArr[j]+" ");
        }
        
        sc.close();
    }
 
}
 
cs

6.

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 Q6 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        
        int unit[]= {50000,10000,1000,100,10,1};
        
        System.out.println("금액을 입력하시오>>");
        int money=sc.nextInt();
        
        for(int i=0;i<unit.length;i++) {
            System.out.println(unit[i]+"원 짜리 : "+money/unit[i]+"원 ");
            money-=(money/unit[i])*unit[i];   //남은돈 계산식
        }
 
    }
 
}
cs

7.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
public class Q7 {
 
    public static void main(String[] args) {
        int arr[] = new int[10];
        
        for(int i=0; i<10; i++) {
            arr[i] = (int)(Math.random()*10+1);
        }
        System.out.print("랜덤한 정수들 : ");
        double sum = 0;
        for (int i=0; i<10; i++) {
            System.out.print(arr[i]+" ");
            sum += arr[i];
        }
        System.out.println("\n평균은 "+sum/10);
        
    }
 
}
 
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
34
35
36
37
import java.util.Scanner;
 
public class Q8 {
 
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.print("정수 몇개?(1~99): ");
        int n = sc.nextInt();
        
        int arr[] = new int[n];
        
        //겹치면 안되니까 그것도 처리해야 합니다. 여러 방법이 있을거에요. 저는 배열을 한번 다 돌아서 겹치는게 없다면 넣겠습니다!
        for (int i=0; i<n;i++) {
            arr[i] = (int)(Math.random()*100+1);
            
            //중복인지 확인. 배열의 첫값부터 지금위치까지 돌았을때 같은값이 있다면 그즉시 반복을 한번 더 한다.
            for(int j = 0; j<i; j++) {
                if(arr[j] == arr[i]) {
                    i--;
                    break;
                }
                
            }
        }
        
        for(int i=0;i<n;i++) {
            if(i%10==9) { 
                System.out.print(arr[i]+" ");
                System.out.println();
            }
            else {
                System.out.print(arr[i]+" ");
            }
        }
    }
 
}
cs

9.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Q9 {
 
    public static void main(String[] args) {
        int[][] arr;
        arr=new int[4][4];
        
        for(int i=0;i<4;i++) {
            for(int j=0;j<4;j++) {
                arr[i][j]=(int)(Math.random()*10+1);
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }
            
    }
 
}
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
32
33
34
public class Q10 {
 
    public static void main(String[] args) {
        int arr[][]=new int[4][4];
        int arr2[] = new int[10];
        int x,y;   //배열의 랜덤한 위치에 생성할것임.
        
        for(int i =0; i<10; i++) {
            arr2[i] = (int)(Math.random()*10+1);
            
        }
        
        for(int i=0; i<10; i++) {
            x=(int)(Math.random()*4);
            y=(int)(Math.random()*4);
            if(arr[x][y]==0) {
                arr[x][y] = arr2[i];
            }
            else {        //만약에 x,y가 0이 아니면 어떤 값이 채워져 있는것이므로 반복문을 한번 더 돌리게 한다!
                i--;
                
            }
        }
        
        for(int i=0;i<arr.length;i++) {
            for (int j =0;j<arr[i].length;j++) {
                System.out.print(arr[i][j]+"\t");
            }
            System.out.println();
        }
        
    }
}
 
cs

11.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.util.Scanner;
 
public class Q11 {
 
    public static void main(String[] args) {
        int sum = 0;
        
        for(int i=0;i<3;i++) {
            sum += Integer.parseInt(args[i]);
        }
        System.out.println(sum/3);
 
    }
 
}
 
cs

12.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Scanner;
 
public class Q12 {
 
    public static void main(String[] args) {
        int sum = 0;
        
        for(int i = 0; i<args.length; i++) {
            try {
                sum += Integer.parseInt(args[i]);
            }catch(NumberFormatException e){
                e.printStackTrace();
            }
        }
        System.out.println(sum);
    }
 
}
 
cs

13.

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
public class Q13 {
 
    public static void main(String[] args) {
        
        
        for(int i=0; i<100; i++) {
            int cnt = 0//박수 횟수 셀겁니다.
            
            if(i%10==3 || i%10==6|| i%10==9) {
                cnt +=1;
            }
            if(i/10 ==3 || i/10 == 6 || i/10 == 9) {
                cnt+=1;
            }
            
            if(cnt == 1) {
                System.out.println(i+ " 박수 짝");
            }else if(cnt == 2) {
                System.out.println(i+ " 박수 짝짝");
            }
        }
 
    }
 
}
 
cs

14.

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 Q14 {
 
    public static void main(String[] args) {
        String course[] = {"Java""C++""HTML5""컴퓨터구조""안드로이드"};
        int score[] = {9588766255};
        Scanner sc = new Scanner(System.in);
        
        while(true) {
            System.out.print("과목 이름(그만 입력시 종료)>>");
            String input = sc.next();
            if(input.contentEquals("그만")) {
                System.out.println("종료...");
                break;
            }
            for (int i=0; i<course.length; i++) {
                if(input.contentEquals(course[i])) {
                    System.out.println(course[i]+"의 점수는 "+score[i]);
                    break;
                }
                if(i==course.length-1) {
                    System.out.println("없는 과목입니다.");
                }
            }
        }
    }    
}
 
cs

15.

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
import java.util.InputMismatchException;
import java.util.Scanner;
 
public class Q15 {
 
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(true) {
            System.out.print("곱하고자 하는 두 수 입력>>");
            try {
                
                int n = scanner.nextInt();
                int m = scanner.nextInt();
                System.out.println(n+ "x"+ m+ "="+ n*m);
                break;
            }catch(InputMismatchException e) {
                System.out.println("실수는 입력하면 안됩니다.");
                scanner.nextLine();   //입력 받은거 삭제
            }
            
        }
        scanner.close();
    }
 
}
 
cs

16.

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.util.Scanner;
 
public class Q16 {
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.println("컴퓨터와 가위 바위 보 게임을 합니다.");
        int win=0, lose=0, draw=0;   //나중에 이긴횟수 진횟수 알려줄거빈다.
        
        while(true) {
            System.out.println("가위 바위 보!>>");
            String user = sc.next();
            if(user.equals("그만")) {
                break;
            }
            int com =(int)(Math.random()*3+1);
            switch (com) {
            case 1://가위라고 생각
                if(user.equals("가위")) {
                    System.out.println("사용자:가위, 컴퓨터:가위 비겼습니다.");
                    draw++;
                }else if(user.equals("바위")) {
                    System.out.println("사용자:바위, 컴퓨터:가위 이겼습니다.");
                    win++;
                }else if(user.equals("보")) {
                    System.out.println("사용자:보, 컴퓨터:가위 졌습니다.");
                    lose++;
                }
                else {
                    System.out.println("제대로 다시 입력하세요.");
                }
                break;
            case 2://바위라고 생각
                if(user.equals("가위")) {
                    System.out.println("사용자:가위, 컴퓨터:바위 졌습니다.");
                    lose++;
                }else if(user.equals("바위")) {
                    System.out.println("사용자:바위, 컴퓨터:바위 비겼습니다.");
                    draw++;
                }else if(user.equals("보")) {
                    System.out.println("사용자:보, 컴퓨터:바위 이겼습니다.");
                    win++;
                }
                else {
                    System.out.println("제대로 다시 입력하세요.");
                }
                break;
            case 3://보라고 생각
                if(user.equals("가위")) {
                    System.out.println("사용자:가위, 컴퓨터:보 이겼습니다.");
                    win++;
                }else if(user.equals("바위")) {
                    System.out.println("사용자:바위, 컴퓨터:보 졌습니다.");
                    lose++;
                }else if(user.equals("보")) {
                    System.out.println("사용자:보, 컴퓨터:보 비겼습니다.");
                    draw++;
                }
                else {
                    System.out.println("제대로 다시 입력하세요.");
                }
                break;
            }
            
        }
        System.out.println("이긴횟수: "+win);
        System.out.println("진횟수: "+lose);
        System.out.println("비긴횟수: "+draw);
 
    }
 
}
 
cs