중첩if문을 사용하여 A와 B를 입력받아 가위바위보를 하는 코드를 만들어 보겠다.
우선 입력받아야하므로
1) Scanner를 사용해
2) 입력받을 값이 2개일 것이고,
3) "가위", "바위", "보" 3개의 값만을 받고 이외의 값이 들어오면 잘 못 낸 행동이라고 알려줘야할 것이다.
이것을 바탕으로 아래와 같이 코드를 짰다.
import java.util.Scanner; //Scanner 클래스를 사용하기 위함.
import java.util.Objects;
public class main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("가위바위보 시작!!");
System.out.println("A값 입력 : ");
String aHand = sc.nextLine();
System.out.println("B값 입력 : ");
String bHand = sc.nextLine();
String result = "";
if (Objects.equals(aHand, "가위")) {
if (Objects.equals(bHand, "가위")) {
result = "비김";
} else if (Objects.equals(bHand, "바위")) {
result = "B가 이김";
} else if (Objects.equals(bHand, "보")) {
result = "A가 이김";
} else {
result = "B가 가위바위보를 하지않음";
}
} else if (Objects.equals(aHand, "바위")) {
if (Objects.equals(bHand, "가위")) {
result = "A가 이김";
} else if (Objects.equals(bHand, "바위")) {
result = "비김";
} else if (Objects.equals(bHand, "보")) {
result = "B가 이김";
} else {
result = "B가 가위바위보를 하지않음";
}
} else if (Objects.equals(aHand, "보")) {
if (Objects.equals(bHand, "가위")) {
result = "B가 이김";
} else if (Objects.equals(bHand, "바위")) {
result = "A가 이김";
} else if (Objects.equals(bHand, "보")) {
result = "비김";
} else {
result = "B가 가위바위보를 하지않음";
}
} else {
result = "A가 가위바위보를 하지않음";
}
System.out.println(result);
}
}
작성하면서도 느꼈지만 너무 중복되는 코드가 많다고 생각되어 3가지 방법으로 나누어 보려고 한다.
A가 이기는 경우, B가 이기는 경우, 비기는 경우 이렇게 3가지로!
if (Objects.equals(aHand, bHand)){
result = "비김";
}else if ((Objects.equals(aHand,"가위") && Objects.equals(bHand,"보")) ||
(Objects.equals(aHand,"바위") && Objects.equals(bHand,"가위")) ||
(Objects.equals(aHand,"보") && Objects.equals(bHand,"바위"))) {
result = "A 승리";
} else if ((Objects.equals(aHand,"가위") && Objects.equals(bHand,"바위")) ||
(Objects.equals(aHand,"바위") && Objects.equals(bHand,"보")) ||
(Objects.equals(aHand,"보") && Objects.equals(bHand,"가위"))) {
result = "B 승리";
} else {
result = "가위, 바위, 보 중 하나를 내세요";
}
이나저나 비슷하려나..? 그래도 내 기준으로 result에 대입하는 부분 자체를 3가지로만 만드니 보기는 편해진것 같다.
여기에서 더 해 A는 컴퓨터가 가위바위보중 랜덤으로 하나를 내주고, B는 내가 직접 하는 코드를 짜보려고 한다!
여기서는 컴퓨터가 직접 내는 가위바위보를 Math.random()함수를 사용해서 나타내려고 한다.
Math.random같은 경우는 0이상 1미만의 난수를 생성하는 메서드로 3을 곱하면 0이상 3미만의 난수를 생성할 것이다.
이것을 int로 형변환을 시켜준다면 0일때는 가위, 1일때는 바위, 2일때는 보로 조건을 주어 가위바위보를 만들 수 있을 것이다.
Scanner sc = new Scanner(System.in);
System.out.println("가위바위보 시작!!");
int random = (int) (Math.random() * 3);
String computer = "";
if (random == 0) {
computer = "가위";
} else if (random == 1) {
computer = "바위";
} else if (random == 2) {
computer = "보";
}
System.out.println("사용자 입력 : ");
String user = sc.nextLine();
String result = "";
if (Objects.equals(computer, user)) {
result = "비김";
} else if ((Objects.equals(computer, "가위") && Objects.equals(user, "보")) ||
(Objects.equals(computer, "바위") && Objects.equals(user, "가위")) ||
(Objects.equals(computer, "보") && Objects.equals(user, "바위"))) {
result = "컴퓨터 승리";
} else if ((Objects.equals(computer, "가위") && Objects.equals(user, "바위")) ||
(Objects.equals(computer, "바위") && Objects.equals(user, "보")) ||
(Objects.equals(computer, "보") && Objects.equals(user, "가위"))) {
result = "사용자 승리";
} else {
result = "가위, 바위, 보 중 하나를 내세요";
}
System.out.println("컴퓨터 : " + computer + " 나 : " + user);
System.out.println("결과 : " + result);
위와 같이 코드를 짜보았다! 여러번 돌려봤을 때 난수도 0~2로 아주 잘 생성되고 결과값도 정상적으로 나오는 것을 확인했다.
(오류없겠지..?)