728x90
문제 가위(1), 바위(2), 보(3) 게임 [실행결과] 가위(1),바위(2),보(3) 중 번호 입력 : 3 (user) 컴퓨터 : 바위 나 : 보자기 You Win!! 가위(1),바위(2),보(3) 중 번호 입력 : 3 (user) 컴퓨터 : 가위 나 : 보자기 You Lose!! 가위(1),바위(2),보(3) 중 번호 입력 : 1 (user) 컴퓨터 : 가위 나 : 가위 You Draw!! |
BufferedReader 사용
package if_Ex;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class RPSGame {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int com, user;
String userRPS = null, comRPS = null;
System.out.print("가위(1), 바위(2), 보(3) 게임중\n"
+ "번호입력 : ");
user = Integer.parseInt(br.readLine()); //user값 입력
com = (int) (Math.random() * 3) + 1; // 1~3난수발생
//com의 난수(int)1~3을 차례대로 comRPS(String)에 가위,바위,보 저장하기
if (com == 1) comRPS = "가위";
else if (com == 2) comRPS = "바위";
else if (com == 3) comRPS = "보";
//user의 입력값 (int)1~3을 차례대로 userRPS(String)에 가위바위보 저장하기
if (user == 1) userRPS = "가위";
else if (user == 2) userRPS = "바위";
else if (user == 3) userRPS = "보";
System.out.println("[실행결과]");
System.out.println("컴퓨터 : " + comRPS + ", 나 : " + userRPS);
if (com == 1) {//com이 1(가위)일때
if (user == 2) {
System.out.println("You Win!!");
} else if (user == 1) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
} else if (com == 2) {//com이 2(바위)일때
if (user == 3) {
System.out.println("You Win!!");
} else if (user == 2) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
} else { //com이 3(보)일때
if (user == 1) {
System.out.println("You Win!!");
} else if (user == 3) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
}
}
}
d컴퓨터의 값은 Math.random()을 이용한다.
random은0~1사이의 임의의 수를 만들어 내는데
random()*100을 하면 0~100까지의 수를 만들어낸다.
random()*3+1을 하면
0<x<1 ==> 0<x3< ==> 1<x<4로 바꾼다.
a~b사이의 난수를 만드는 방법 rnadom()*(b-a)+a을 기억하자
BufferedReader의 readLine(String타입)을 썼기 때문에 정수를 받기 위해 Integer.parseInt를 사용해 타입을 변환시킨다.
컴퓨터가 받는값에 따라 string인 가위바위보를 대입시키고
사용자가 입력하는 값에 따라 string인 가위바위보를 대입시킨다.
if를 이용해 각 상황에 따른 결과를 선언한다.
Scanner사용
package if_Ex;
import java.util.Scanner;
public class RPSGame03 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int com, user;
String userRPS = null, comRPS = null;
System.out.print("가위(1), 바위(2), 보(3) 게임중\n"
+ "번호입력 : ");
user = sc.nextInt(); //user값 입력
com = (int) (Math.random() * 3) + 1; // 1~3난수발생
//com의 난수(int)1~3을 차례대로 comRPS(String)에 가위,바위,보 저장하기
if (com == 1) comRPS = "가위";
else if (com == 2) comRPS = "바위";
else if (com == 3) comRPS = "보";
//user의 입력값 (int)1~3을 차례대로 userRPS(String)에 가위바위보 저장하기
if (user == 1) userRPS = "가위";
else if (user == 2) userRPS = "바위";
else if (user == 3) userRPS = "보";
System.out.println("[실행결과]");
System.out.println("컴퓨터 : " + comRPS + ", 나 : " + userRPS);
if (com == 1) {//com이 1(가위)일때
if (user == 2) {
System.out.println("You Win!!");
} else if (user == 1) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
} else if (com == 2) {//com이 2(바위)일때
if (user == 3) {
System.out.println("You Win!!");
} else if (user == 2) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
} else { //com이 3(보)일때
if (user == 1) {
System.out.println("You Win!!");
} else if (user == 3) {
System.out.println("You Draw!!");
} else {
System.out.println("You Lose!!");
}
}
}
}
read사용
System.in.read()를 하면 숫자가 아닌 문자(char)로 입력을 받는다.
사용자가 1~3을 입력하면 컴퓨터는 문자(char)로 인식해서 아스키 코드인 49,50,51를 출력한다.
read로 1~3의 값을 받을려면 0(아스키코드 48)을 빼면 1, 2, 3의 숫자를 받을 수 있다.
package if_Ex;
import java.io.IOException;
public class RPSGame02 {
public static void main(String[] args) throws IOException {
int com, user;
com = (int)(Math.random()*3)+1; //1~3난수 발생
System.out.println("가위(1), 바위(2), 보(3) 게임중+ 번호입력 : ");
user = System.in.read()-'0';
//user = System.in.read()-48;//read는 문자를 입력받는것
//0을 문자로하면 48의 값을 가지기 떄문에 -를 해줘야 1,2,3을 문자로 받아도 값이 나온다.
if(com==1) {
if(user==1) {
System.out.println("컴퓨터 : 가위 \t 사용자 : 가위");
System.out.println("Deaw!!");
}else if(user==2) {
System.out.println("컴퓨터 : 가위 \t 사용자 : 바위");
System.out.println("Win!!");
}else if(user==3) {
System.out.println("컴퓨터 : 가위 \t 사용자 : 보");
System.out.println("Lose!!");
}
}else if(com==2) {
if(user==1) {
System.out.println("컴퓨터 : 바위 \t 사용자 : 가위");
System.out.println("Lose!!");
}else if(user==2) {
System.out.println("컴퓨터 : 바위 \t 사용자 : 바위");
System.out.println("Draw!!");
}else if(user==3) {
System.out.println("컴퓨터 : 바위 \t 사용자 : 보");
System.out.println("Win!!");
}
}else if(com==3) {
if(user==1) {
System.out.println("컴퓨터 : 보 \t 사용자 : 가위");
System.out.println("Win!!");
}else if(user==2) {
System.out.println("컴퓨터 : 보 \t 사용자 : 바위");
System.out.println("Lose!!");
}else if(user==3) {
System.out.println("컴퓨터 : 보 \t 사용자 : 보");
System.out.println("Draw!!");
}
}
}
}
728x90
'JAVA' 카테고리의 다른 글
if, switch 연습(성적구하기) (0) | 2020.09.08 |
---|---|
if문 연습03 (0) | 2020.09.08 |
입력하는방법(Scanner, BufferedReader) (0) | 2020.09.04 |
삼항 연산자 연습 (0) | 2020.09.04 |
10진수를 2,8,16진수로 변환하는 법 (0) | 2020.09.03 |