본문 바로가기
Project/Study | etc

랜덤 카드 추출 게임

by 코젼 2022. 6. 26.
728x90
반응형

🃏랜덤 카드 추출 게임

랜덤으로 카드를 뽑고, 그 카드의 모양과 숫자를 표시하시오.

가로는 100, 세로는 200으로 고정한다.

카드의 모양 = {"heart", "diamond", "clover", "spade"}

카드의 숫자 범위 = 1~13

 

💾소스코드

public class CardTest {
	public static void main(String[] args) {

		String arr[] = { "heart", "diamond", "clover", "spade" };
		Card arr_card[] = new Card[5];

		int num = 0;
		int index = 0;
		for (int i = 0; i < arr_card.length; i++) {
			num = (int) (Math.random() * 13) + 1;
			index = (int) (Math.random() * 4);
			arr_card[i] = new Card(arr[index], num);
			arr_card[i].printCardInfo();

		}
	}
}

class Card {
	String shape;
	int number;
	int height;
	int width;

	Card() {

	}

	Card(String shape, int number, int height, int width) {
		this.shape = shape;
		this.number = number;
		this.height = height;
		this.width = width;

	}

	Card(String shape, int number) {
	      this(shape,number,200,100);
	}

	void printCardInfo() {
		System.out.println("모양 : " + this.shape + "\t숫자 : " + this.number +
				" 가로 : " + this.width + " 세로 : " + this.height);
	}

}

 

728x90
반응형

댓글