내일배움캠프 사전캠프 JAVA 퀘스트 달리기반 보너스 문제
🚀 문제
- 가위바위보 게임 만들기
- 총 5판 진행하며, 승리한 횟수에 따라 경품을 획득
🚀 정답
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
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Random;
public class RpsGiftGame {
public static void main(String[] args) {
// 경품 Map 정의
Map<Integer, String> gifts = new HashMap<>();
gifts.put(0, "꽝");
gifts.put(1, "곰돌이 인형");
gifts.put(2, "스파르타 랜드 입장권");
gifts.put(3, "스파르타 캐니언 항공 투어권");
gifts.put(4, "호텔 스파르타 숙박권");
gifts.put(5, "스파르테이트 항공권");
Scanner scanner = new Scanner(System.in);
Random random = new Random();
int numOfWins = 0; // 사용자의 승리 횟수
int rounds = 5; // 총 게임 횟수
System.out.println("🎮 가위바위보 게임을 시작합니다! 총 " + rounds + "판 진행됩니다.");
for (int i = 1; i <= rounds; i++) {
System.out.println("\n[" + i + "번째 판] 가위, 바위, 보 중 하나를 입력해주세요:");
String userChoice = scanner.nextLine();
// 유효성 검사
if (!(userChoice.equals("가위") || userChoice.equals("바위") || userChoice.equals("보"))) {
System.out.println("❌ 잘못 입력하셨습니다! 이 판은 무효 처리됩니다.");
i--; // 유효하지 않은 입력은 판 수에 포함하지 않음
continue;
}
// 컴퓨터의 선택
String[] rps = {"가위", "바위", "보"};
String computerChoice = rps[random.nextInt(3)];
System.out.println("💻 컴퓨터의 선택: " + computerChoice);
// 승패 판별
if (userChoice.equals(computerChoice)) {
System.out.println("🤝 비겼습니다!");
} else if (
(userChoice.equals("가위") && computerChoice.equals("보")) ||
(userChoice.equals("바위") && computerChoice.equals("가위")) ||
(userChoice.equals("보") && computerChoice.equals("바위"))
) {
System.out.println("🎉 이겼습니다!");
numOfWins++;
} else {
System.out.println("😭 졌습니다!");
}
}
// 최종 결과 출력
String prize = gifts.getOrDefault(numOfWins, "꽝");
System.out.println("\n🎊 축하합니다! 총 " + numOfWins + " 회 승리하여 경품으로 [" + prize + "] 을 획득하셨습니다!");
}
}