June is Combung

1 to 4 본문

java/배열

1 to 4

june__Park 2021. 3. 23. 10:27
package week2;

import java.util.Random;
import java.util.Scanner;

public class Day7_1to4 {
	/*
	 * #  1 to 4
	 * 1. arr배열에 1~4 사이의 숫자를 중복없이 저장한다.
	 * 2. 사용자는 1부터 순서대로 해당 위치 값을 입력한다.
	 * 3. 정답을 맞추면 해당 값은 9로 변경되어 모든 값이 9가 되면 게임은 종료된다.
	 * 예)
	 * 4 2 3 1
	 * 입력 : 3
	 * 4 2 3 9
	 * 입력 : 1
	 * 4 9 3 9
	 * ...
	 */
	public static void main(String[] args) {
		int[] arr = new int[4];
		int[] check = new int[4];
		
		Random rand = new Random();
		Scanner sc = new Scanner(System.in);
		int i = 0;
		int count = 0;
		
		while(i<arr.length) {
			int num = rand.nextInt(4)+1;
			if(check[num-1]==0) {
				check[num-1] = 1;
				arr[i]=num;
				i++;
			}
		}
		
		while(true) {
			int count2 = 0;
			for(int j = 0; j < arr.length; j++) {
				System.out.print(arr[j]+" ");
			}
			System.out.println();
			for(int j = 0; j < arr.length; j++) {
				if(arr[j]==9) {
					count2++;
				}
			}
			if(count2==4) {
				System.out.println("게임종료.");
				break;
			}
			
			System.out.print("입력 : ");
			int input = sc.nextInt();
			if(count == 0) {
				if(arr[input-1]==1) {
					arr[input-1] = 9;
					count++;
				}
				else {
					System.out.println("틀렸습니다.");
				}
			}
			else if(count == 1) {
				if(arr[input-1]==2) {
					arr[input-1] = 9;
					count++;
				}
				else {
					System.out.println("틀렸습니다.");
				}
			}
			else if(count == 2) {
				if(arr[input-1]==3) {
					arr[input-1] = 9;
					count++;
				}
				else {
					System.out.println("틀렸습니다.");
				}
			}
			else if(count == 3) {
				if(arr[input-1]==4) {
					arr[input-1] = 9;
					count++;
				}
				else {
					System.out.println("틀렸습니다.");
				}
			}
		}
		
		sc.close();
	}

}

'java > 배열' 카테고리의 다른 글

사다리게임  (0) 2021.03.23
틱택토  (0) 2021.03.23
1 to 50  (0) 2021.03.23
기억력 게임  (0) 2021.03.23
배열 기본문제  (0) 2021.03.23
Comments