본문 바로가기
(JAVA)자바/(JAVA)자바 실습

(JAVA)자바 가위바위보 게임 프로그램- random함수를 활용한 자바 실습

by 공부가싫다가도좋아 2021. 1. 28.
반응형

가위바위보 프로그램


 

math.random함수를 활용하여 간단한 가위바위보 게임을 만들어보았습니다.

 

import java.util.*;

public class Rocspaper {
		
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		String[] a= {"가위","주먹","보"};
		System.out.println(Arrays.toString(a));
		
		for(int i=0;i<10;i++) {
			int num= (int)(Math.random()*3);
			System.out.println("가위,바위,보 중 하나를 선택");
			System.out.print(">>>");
			String b=scan.next();
			System.out.println();
			if(b.equals("가위")) {
				if(num==0) {
					System.out.println("나:가위");
					System.out.println("상대편:바위");
					System.out.println("졌습니다");
				}
				if(num==1) {
					System.out.println("나:가위");
					System.out.println("상대편:보");
					System.out.println("이겼습니다");
				}
				if(num==2) {
					System.out.println("나:가위");
					System.out.println("상대편:가위");
					System.out.println("비겼습니다");
				}
				System.out.println();
			}
			if(b.equals("바위")) {
				if(num==0) {
					System.out.println("나:바위");
					System.out.println("상대편:바위");
					System.out.println("비겼습니다");
				}
				if(num==1) {
					System.out.println("나:바위");
					System.out.println("상대편:보");
					System.out.println("졌습니다");
				}
				if(num==2) {
					System.out.println("나:바위");
					System.out.println("상대편:가위");
					System.out.println("이겼습니다");
				}
				System.out.println();
			}
			if(b.equals("보")) {
				if(num==0) {
					System.out.println("나:보");
					System.out.println("상대편:바위");
					System.out.println("이겼습니다");
				}
				if(num==1) {
					System.out.println("나:보");
					System.out.println("상대편:보");
					System.out.println("비겼습니다");
				}
				if(num==2) {
					System.out.println("나:보");
					System.out.println("상대편:가위");
					System.out.println("졌습니다");
				}
				System.out.println();
			}
			
			
			
		}//for(int i=0;i<10;i++)
		
		
		

	}

}
반응형

댓글