-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHello_07.java
More file actions
69 lines (58 loc) · 1.53 KB
/
Hello_07.java
File metadata and controls
69 lines (58 loc) · 1.53 KB
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
package study;
import java.util.Scanner;
public class Hello_07 {
public static void main(String[] args) {
// if 안의 값을 밖으로 꺼내는법
System.out.print("가위1 바위2 보3 중 하나를 입력하시오 >>> ");
Scanner sc = new Scanner(System.in);
String userSt = sc.next();
int com = (int) (Math.random() * 3) + 1; // Math.random 은 원래 double 형입니다!
System.out.println("처음 com 값이다: " + com);
boolean equals1 = userSt.equals("가위");
boolean equals2 = userSt.equals("바위");
boolean equals3 = userSt.equals("보"); // if 값 비교는 boolean 으로 한다.
int user = 0; // 이렇게 미리 선언하면 if 안의값을 밖으로 꺼낼 수 있다.
String comSt =null;
// 또는 String comSt =""; 이렇게 선언
// String comSt; 하면 오류생김
if (equals1) {
user = 1;
}
if (equals2) {
user = 2;
}
if (equals3) {
user = 3;
}
switch(com) {
case 1:
comSt="가위";
break;
case 2:
comSt="바위";
break;
case 3:
comSt="보";
break;
}
/*
* if(com ==1) { comSt="가위"; } if(com == 2) { comSt="바위"; } if(com ==3) {
* comSt="보"; }
*/
System.out.println("내가 고른거>> " + userSt + user);
System.out.println("컴터 >> " + comSt + com);
switch (user - com) {
case 1:
case -2:
System.out.println("내가 이겼어요");
break;
case 0:
System.out.println("비겼어요");
break;
default:
System.out.println("내가 졌어요");
break;
}
sc.close();
}
}