Java를 이용한 동전 앞뒤 맞추기

2021. 7. 17. 00:54
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
package Coin;
 
import java.util.Random;
import java.util.Scanner;
 
public class CoinCoin {
 
    public static void main(String[] args) {
        
        System.out.println("동전 앞뒤 맞추기를 시작합니다!");
        
        Scanner scan     = new Scanner(System.in);
        
        String choice;
        String OddEven_num;
        
        double win         = 0;
        double defeat     = 0;
 
        Boolean asker     = true;
        
        while(asker) {
        
            Random random     = new Random();
            Boolean OddEven = random.nextBoolean();
                        
            if(false == OddEven) {
                OddEven_num = "1";
            } else {
                OddEven_num = "2";
            }
            
            System.out.println("1번을 입력 시 앞면, 2번을 입력 시 뒷면입니다. 3번을 입력 시 종료됩니다.");
            choice = scan.nextLine();
            
            if(choice.equals("3")) {
                asker = false;
            } else if(!choice.equals("1"&& !choice.equals("2"&& !choice.equals("3") ) {
                System.out.println("잘못 입력하셨습니다. 다시 입력하십시오!");    
            } else if(choice.equals(OddEven_num)) {
                System.out.println("축하합니다. 정답입니다!");
                win += 1;
            } else {
                System.out.println("틀렸습니다ㅠㅠㅠㅠㅠㅠ");
                defeat += 1;
            }    
        }
        
        System.out.println("게임을 종료합니다.");
        
        if1 <= (win+defeat)) {
            System.out.println("모든 게임이 끝났습니다. 결과는 다음과 같습니다.");
            System.out.println("총 반복 횟수 : " + (win+defeat) + "번");
            System.out.println("승률 : " + (double)(win * 100 / (win + defeat)) + "%");
        }
 
        scan.close();
        return;
    }
}
 
cs

 

cointest.MP4
0.61MB

 

Code Review

  C언어에서부터 srand 등으로 사용하던 기존의 Math클래스를 이용한

   " (int)(Math.random() * 최대값 - 최소값 + 1) * 최소값 "

   과는 다른, java 내에 내장된 Random 클래스를 사용해보았다.

   I used 'Random Class', which is different from the 'traditional Math Class format' used in the C language.

 

BELATED ARTICLES

more