728x90
문제조건
1. 0은 주먹, 1은 보, 2는 가위
2. 컴퓨터는 무작위로 숫자를 선택하고, 사용자에게는 숫자를 입력받는다.
3. 가위바위보게임은 무한 반복한다.
알고리즘
1. Math.random함수로 컴퓨터가 낼 패를 랜덤으로 정한다.
2. 사용자의 패는 0,1,2로 지정한다.
3. while문으로 가위바위보를 무한 반복한다.
4. 사용자의 선택에 따라 게임을 종료할 수 있게 추가했다.
5. 사용자가 잘못된 숫자를 입력할 경우에는 경고문구를 출력하고 숫자를 다시 입력을 받는다.
public class iam_im2 {
public static void main(String[] args) {
RockPaperScissor();
}
public static void RockPaperScissor() {
Scanner sc = new Scanner(System.in);
int exit = 0;
while (exit == 0) {
int randomNum = (int) (Math.random() * 3);
System.out.println("0: Rock, 1: Paper, 2: Scissor");
System.out.print("Enter the number : ");
int input = sc.nextInt();
if (input == 0 || input == 1 || input == 2) {
// 컴퓨터 패
if (randomNum == 0) {
System.out.println("Computer : Rock");
} else if (randomNum == 1) {
System.out.println("Computer : Paper");
} else if (randomNum == 2) {
System.out.println("Computer : Scissor");
}
// 플레이어패
if (input == 0) {
System.out.println("Player : Rock");
} else if (input == 1) {
System.out.println("Player : Paper");
} else if (input == 2) {
System.out.println("Player : Scissor");
}
// 승패판정
if ((randomNum == 0 && input == 1) || (randomNum == 1 && input == 2)
|| (randomNum == 2 && input == 0)) {
System.out.println("Player wins!\n");
} else if ((randomNum == 0 && input == 2) || (randomNum == 1 && input == 0)
|| (randomNum == 2 && input == 1)) {
System.out.println("Computer wins!\n");
} else if (randomNum == input) {
System.out.println("Player and computer draw.\n");
}
} else {
System.out.println("Check the number.\n");
System.out.println("");
continue;
}
// 게임 종료 여부
int continueChoice = 0;
while (continueChoice == 0) {
System.out.println("Do you want continue the game?");
System.out.println("1: YES , 2: NO");
System.out.print("Enter the number : ");
int choice = sc.nextInt();
if (choice == 1) {
exit = 0;
continueChoice++;
} else if (choice == 2) {
exit = 1;
continueChoice++;
System.out.println("Bye Bye~");
} else {
System.out.println("Check the number!");
System.out.println("");
continue;
}
System.out.println("");
}
} // while
}
} // class
출력결과 |
0: Rock, 1: Paper, 2: Scissor Enter the number : 0 Computer : Rock Player : Rock Player and computer draw. Do you want continue the game? 1: YES , 2: NO Enter the number : 1 0: Rock, 1: Paper, 2: Scissor Enter the number : 3 Check the number. 0: Rock, 1: Paper, 2: Scissor Enter the number : 1 Computer : Rock Player : Paper Player wins! Do you want continue the game? 1: YES , 2: NO Enter the number : 3 Check the number! Do you want continue the game? 1: YES , 2: NO Enter the number : 2 Bye Bye~ |
우리나라에서는 가위,바위,보(Scissors-Rock-Paper) 라고 하는데 영미권에서는 바위,보,가위(Rock-Paper-Scissors)다. 평소에 아무생각없이 Rock-Paper-Scissors 라고 했는데, 코딩하면서 패의 순서가 이상하다는 것을 알게됐다. 가위바위보! 하다가 바위보가위! 하면 되게 이상하지않나..? 서로 사용하는 언어가 달라서 그런가보다. 그래도 역시 가위바위보!✌👊🖐가 좋다.
728x90
'언어 > JAVA' 카테고리의 다른 글
for each문 (0) | 2022.04.20 |
---|---|
Eclipse 이클립스 폰트 변경 (0) | 2022.04.14 |
숫자를 입력 받는 Timer (0) | 2022.04.07 |
currentTimeMillis() 함수: 소요시간 계산 (0) | 2022.04.07 |
Arrays.sort 로 배열 정렬하기 (0) | 2022.04.06 |