T_era

[JAVA] 문제풀이 : 가위바위보 본문

Programing/Java

[JAVA] 문제풀이 : 가위바위보

블스뜸 2025. 3. 26. 17:16
5번의 가위바위보 게임을 해서 승리한 횟수 상당의 상품을 받아가는 게임을 만들어봐요
 
실행 화면 예시
1.
5번의 가위바위보를 진행합니다.
//1번문제
        String str = sc.nextLine();
        for(int i = 0; i < 5; i++){

        }
2.
유저는 매 판마다 “가위”, “바위”, “보” 중 하나를 입력합니다.
잘못된 입력을 받았다면 잘못된 입력입니다! 를 출력해주세요.
    // 2번문제
    public boolean Accuracy(String _str){
        boolean flag = false;

        if(_str.isEmpty()) return flag;

        for(String s : cpu){
            if(s.equals(_str)) flag = true;
        }
        return flag;
    }
3.
컴퓨터는 가위 , 바위, 보 중 랜덤하게 하나를 낼 수 있습니다.
    // 3번 문제
    public String CpuHand(){
        Random rand = new Random();
        rand.setSeed(System.currentTimeMillis());

        return cpu[rand.nextInt(3)];
    }
 
4.
매판마다 진행한 가위 바위 보의 승패에 대한 결과를 출력됩니다.
// 4번 문제
    public String Confrontation(String strCpu, String strPlayer){
        String result = "";
        switch (strCpu){
            case "가위":{
                if(strPlayer.equals("가위")) result = "비겼습니다";
                else if(strPlayer.equals("바위")) result = "이겼습니다";
                else result = "졌습니다";
            }break;
            case "바위":{
                if(strPlayer.equals("가위")) result = "졌습니다";
                else if(strPlayer.equals("바위")) result = "비겼습니다";
                else result = "이겼습니다";
            }break;
            case "보":{
                if(strPlayer.equals("가위")) result = "이겼습니다";
                else if(strPlayer.equals("바위")) result = "졌습니다";
                else result = "비겼습니다";
            }break;
            default :
        }
        System.out.println("플레이어가 " + result);
        return result;
    }
 
5.
5판을 모두 마치면 승리한 횟수에 걸맞는 경품을 획득할 수 있습니다.
 
// 5번문제
    public void Gift(int cnt){
        System.out.println(cnt + "번 이겼습니다.");
    }

 

전체코드

import java.lang.classfile.instruction.SwitchCase;
import java.util.Random;
import java.util.Scanner;

public class HandGame {
    private final String[] cpu = {"가위", "바위", "보"};
    public static void main(String[] args) {
        HandGame hg = new HandGame();
        Scanner sc = new Scanner(System.in);
        int winCount = 0;

        //1번문제

        for(int i = 0; i < 5; i++){
            System.out.print("입력 : ");
            String str = sc.nextLine();
            if(!hg.Accuracy(str)){
                System.out.println("잘못된 입력입니다");
                continue;
            }
            String cpuHand = hg.CpuHand();
            String result = hg.Confrontation(cpuHand, str);
            if(result.equals("이겼습니다")) winCount++;
        }
        hg.Gift(winCount);
    }
    // 2번문제
    public boolean Accuracy(String _str){
        boolean flag = false;

        if(_str.isEmpty()) return flag;

        for(String s : cpu){
            if(s.equals(_str)) flag = true;
        }
        return flag;
    }
    // 3번 문제
    public String CpuHand(){
        Random rand = new Random();
        rand.setSeed(System.currentTimeMillis());

        return cpu[rand.nextInt(3)];
    }
    // 4번 문제
    public String Confrontation(String strCpu, String strPlayer){
        String result = "";
        switch (strCpu){
            case "가위":{
                if(strPlayer.equals("가위")) result = "비겼습니다";
                else if(strPlayer.equals("바위")) result = "이겼습니다";
                else result = "졌습니다";
            }break;
            case "바위":{
                if(strPlayer.equals("가위")) result = "졌습니다";
                else if(strPlayer.equals("바위")) result = "비겼습니다";
                else result = "이겼습니다";
            }break;
            case "보":{
                if(strPlayer.equals("가위")) result = "이겼습니다";
                else if(strPlayer.equals("바위")) result = "졌습니다";
                else result = "비겼습니다";
            }break;
            default :
        }
        System.out.println("컴퓨터 : " + strCpu + "\t플레이어 : " + strPlayer);
        System.out.println("플레이어가 " + result);
        return result;
    }
    // 5번문제
    public void Gift(int cnt){
        System.out.println(cnt + "번 이겼습니다.");
    }
}