문제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
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
package whileex;
 
import javax.swing.JOptionPane;
 
public class WhileTest05 {
    public static void main(String[] args) {
 
        // 가위 바위 보
 
        String msg = "가위 바위 보!\n";
        msg += "가위: 0, 바위: 1, 보: 2, 종료: 3";
 
        int exit = 0;
 
        while (true) {
 
            if (exit == -1)
                break;
 
            int input = Integer.parseInt(JOptionPane.showInputDialog(msg));
            int answer = (int) (Math.random() * 10); // 0~9
 
            if (<= answer && answer <= 2) {
                // 가위
                answer = 0;
            } else if (<= answer && answer <= 6) {
                // 바위
                answer = 1;
            } else {
                // 보
                answer = 2;
            }
 
            switch (input) {
            case 0// 가위
                if (answer == 0)
                    JOptionPane.showMessageDialog(null"컴퓨터: 가위\n비겼습니다.");
                else if (answer == 1)
                    JOptionPane.showMessageDialog(null"컴퓨터: 바위\n졌습니다.");
                else
                    JOptionPane.showMessageDialog(null"컴퓨터: 보\n이겼습니다.");
                break;
 
            case 1// 바위
                if (answer == 0)
                    JOptionPane.showMessageDialog(null"컴퓨터: 가위\n이겼습니다.");
                else if (answer == 1)
                    JOptionPane.showMessageDialog(null"컴퓨터: 바위\n비겼습니다.");
                else
                    JOptionPane.showMessageDialog(null"컴퓨터: 보\n졌습니다.");
                break;
 
            case 2// 보
                if (answer == 0)
                    JOptionPane.showMessageDialog(null"컴퓨터: 가위\n졌습니다.");
                else if (answer == 1)
                    JOptionPane.showMessageDialog(null"컴퓨터: 바위\n이겼습니다.");
                else
                    JOptionPane.showMessageDialog(null"컴퓨터: 보\n비겼습니다.");
                break;
 
            case 3:
                exit = -1;
                break;
 
            default:
                JOptionPane.showMessageDialog(null"다시 입력해주세요.");
            } // switch
 
        }
    }
}
cs




문제2. 무한반복을 하다가 사용자가 정답을 맞추면 프로그램을 종료시키자.

   1) 1부터 10 사이의 임의의 랜덤 숫자 발생

   2) 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
35
36
37
38
package whileex;
 
import javax.swing.JOptionPane;
 
public class WhileTest06 {
    public static void main(String[] args) {
 
        // Math.random()
        // 0.0 < Math.random() < 1.0: 실수형
        // (int)Math.random() > 곱하기 10 = 0;
 
        int answer = (int) (Math.random() * 10 + 1);
 
        int chance = 0;
        System.out.println(answer);
 
        while (true) {
 
            int input = Integer.parseInt(JOptionPane.showInputDialog("1에서 10사이의 정수를 입력하세요."));
 
            if (answer < input) {
                JOptionPane.showMessageDialog(null"Down!");
            } else if (answer > input) {
                JOptionPane.showMessageDialog(null"Up!");
            } else {
                JOptionPane.showMessageDialog(null"Bingo!");
                break;
            } // if
 
            chance++;
            if (chance == 3) {
                JOptionPane.showMessageDialog(null"3번의 기회가 끝났습니다!");
                break;
            }
 
        } // while
    }
}
cs