Dev/Java
Java 가위바위보 게임 ( Math.random(), System.in.read() )
surimi🍥
2020. 9. 8. 10:31
반응형
package if_switch;
/*
가위(1), 바위(2), 보(3) 게임
[실행결과]
가위(1),바위(2),보(3) 중 번호 입력 : 3 (user)
컴퓨터 : 바위 나 : 보자기
You Win!!
가위(1),바위(2),보(3) 중 번호 입력 : 3 (user)
컴퓨터 : 가위 나 : 보자기
You Lose!!
가위(1),바위(2),보(3) 중 번호 입력 : 2 (user)
컴퓨터 : 가위 나 : 가위
You Draw!!
*/
import java.io.IOException;
//import java.io.BufferedReader;
//import java.io.InputStreamReader;
public class RPSGame {
public static void main(String[] args) throws IOException {
// BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int user_Input;
int com_Input;
System.out.println("*** 가위, 바위, 보 게임 ***");
System.out.println("가위(1),바위(2),보(3) 중 번호 입력 : ");
System.out.println(">");
// user_Input = Integer.parseInt(br.readLine()); 아래와 같음
user_Input = System.in.read() - '0'; // 문자를 아스키코드로 받아 1을 입력하면 49가 출력되므로 -'0'를 넣어 48을 빼준다
com_Input = (int)(Math.random()*3+1); //1 ~ 3 사이의 난수
if(user_Input == 1) {
if(com_Input == 2) {
System.out.println("컴퓨터 : 바위 나 : 가위");
System.out.println("You Lose!!");
}
else if (user_Input == 3){
System.out.println("컴퓨터 : 보 나 : 가위");
System.out.println("You Win!!");
}
else {
System.out.println("컴퓨터 : 가위 나 : 가위");
System.out.println("Draw!!");
}
}
else if(user_Input == 2) {
if(com_Input == 1) {
System.out.println("컴퓨터 : 가위 나 : 바위");
System.out.println("You Win!!");
}
else if (com_Input == 3){
System.out.println("컴퓨터 : 보 나 : 바위");
System.out.println("You Lose!!");
}
else {
System.out.println("컴퓨터 : 바위 나 : 바위");
System.out.println("Draw!!");
}
}
else if(user_Input == 3) {
if(com_Input == 2) {
System.out.println("컴퓨터 : 바위 나 : 보");
System.out.println("You Win!!");
}
else if(com_Input == 1) {
System.out.println("컴퓨터 : 가위 나 : 보");
System.out.println("You Lose!!");
}
else {
System.out.println("컴퓨터 : 보 나 : 보");
System.out.println("Draw!!");
}
}
}
}
반응형