본문 바로가기
Programming | Study/Java

컴퓨터랑 가위 바위 보 하기

by jinju 2021. 3. 23.

코드 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
import java.util.*;
 
public class Game2 {
    public static void main(String[] args) {
      Scanner sc = new Scanner(System.in);
    
      String str[] = {"가위""바위""보"};
      String me, com;
         int c = 0
         
              System.out.println("가위 바위 보 게임을 시작합니다.");
         while(true) {
               System.out.print("가위 바위 보 중 한 개를 입력하세요 :  ");
          me = sc.next();
                 c = (int)(Math.random()*3);
           com=str[c];
             if(me.equals("가위")) {
               if(str[c].equals("가위")) {
                   System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+"비겼습니다. 다시 시작합니다.");
                      continue;
                    }
              else if(str[c].equals("바위")) {
                   System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 졌습니다.");
                   break;
               }else if(str[c].equals("보")) {
                   System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 이겼습니다!");
                   break;
                    }
              } else if(me.equals("바위")) {
                   if(str[c].equals("가위")) {
                   System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 이겼습니다!");
                   break;
              }else if(str[c].equals("바위")) {
                   System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+"비겼습니다. 다시 시작합니다.");
                   continue;
              }else if(str[c].equals("보")) {
                    System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 졌습니다");
                    break;  
                    }
                   
                 }  else if(me.equals("보")) {
                    if(str[c].equals("가위")) {
                       System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 졌습니다.");
                       break;
             }else if(str[c].equals("바위")) {
                       System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 당신이 이겼습니다!");
                       break;
             }else if(str[c].equals("보")) {
                       System.out.println("당신은 "+me+"를 냈습니다 "+"\n"+"컴퓨터는 "+com+"를 냈습니다"+"\n"+" 다시 시작합니다.");
                       continue;
                 }
                 }
               
         }
        
 
}
}
cs

결과 값

 

 

비겼을 시 다시 할 수 있고 승패가 나면 종료됨

 

 

코드 2 망한거,,,,

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
import java.util.*;
 
public class Game2 {
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        int num;
        int s = 0;
        String input;
        Random random = new Random();
        num = random.nextInt(3);
        
        System.out.println("가위바위 보 입력 : ");
        input = sc.nextLine();
        
        if(input.equals("가위")) {
            s =0;
            
        }else if(input.equals("바위")) {
            s=1;
        }else if(input.equals("보")) {
            s=2;
        }
        
        if(num == 0) {
            System.out.println("컴퓨터는 가위를 냈습니다");
        }else if(num == 1) {
            System.out.println("컴퓨터는 바위를 냈습니다");
        }else if(num == 2) {
            System.out.println("컴퓨터는 보를 냈습니다");
            System.out.println();
        }
        
        switch(s) {
        case 0 : if(num==0) {
            System.out.println("비겼습니다.");
        }else if(num == 1) {
            System.out.println("졌습니다.");
        }else if(num == 2) {
            System.out.println("이겼습니다.");
        
        }
        break;
        
        case 1 : if(num==0) {
            System.out.println("비겼습니다.");
            
        }else if(num == 1) {
            System.out.println("졌습니다.");
        }else if(num == 2) {
            System.out.println("이겼습니다.");
    
        }
        
        break;
    
        case 2 : if(num==0) {
            System.out.println("비겼습니다.");
            return;
        }else if(num == 1) {
            System.out.println("졌습니다.");
        }else if(num == 2) {
            System.out.println("이겼습니다.");
 
        }
        break;
        }
        
        }
    }
 
    
 
cs

 

 

망한거 결과 값 이제보면 while문도 없음 

옛날에 한거라 무슨생각으로 했는지 모르겠다

 

 

'Programming | Study > Java' 카테고리의 다른 글

[Java] txt파일 읽기  (0) 2021.08.18
계산기 프로그램  (2) 2021.03.23
스케줄 관리 프로그램  (0) 2021.03.23
세계나라 수도 사전&퀴즈  (0) 2021.03.23
카드 번호 맞추기  (2) 2021.03.23

댓글