June is Combung
1 to 4 본문
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();
}
}
Comments