java/배열
1 to 50
june__Park
2021. 3. 23. 10:30
package week2;
import java.util.Random;
import java.util.Scanner;
public class Day8_1 {
public static void main(String[] args) {
// 1 - 50
// front 1- 25
// back 26 - 50
// 변수는 변할 수 있는 수
// 상수는 한번 초기값 생성 했으면 변경 할 수 없다 : 모두 대문자
final int SIZE = 25;
Random rand = new Random();
Scanner scan = new Scanner(System.in);
// SIZE = 10; 변경할 수 없다
int front[] = new int[SIZE]; // 0 0 0 0 0 0...
int back[] = new int[SIZE]; // 0 0 0 0 ..
for (int i = 0; i < SIZE; i++) {
front[i] = i + 1; // 1-- 25
back[i] = i + 26; // 26 - 50
}
// 셔플 무작위로 배열 크기안에서 값 가져와서 첫번째 값이랑 교환 : 카드 섞기 1000번 섞었다
int i = 0;
while (i < 100) { // 0 - 24
int rNum = rand.nextInt(SIZE);
int temp = front[0];
front[0] = front[rNum];
front[rNum] = temp;
rNum = rand.nextInt(SIZE);
temp = back[0];
back[0] = back[rNum];
back[rNum] = temp;
i++;
}
int gameNum = 1; // 사용자가 해당 배열에 인덱스에 있는 값이랑 일치했으면 그때 게임값 1 증가
int idx = -1; // 정답을 미리 넣어놈
while (gameNum <= 50) {
for (int j = 0; j < SIZE; j++) {
if (gameNum == front[j]) {
idx = j;
}
if (j % 5 == 0 && j != 0) {
System.out.println(); // 한줄 띄어쓰기
}
System.out.print(" [" + front[j] + "]");
}
System.out.println();
System.out.println("정답 인덱스 [" + idx + "]");
System.out.println(gameNum + "의 인덱스 입력 >> ");
int input = scan.nextInt();
if (idx == input) {
if (gameNum > SIZE) { // 이미 25 반복하면 back 값들이 front 전부 넘어갔음
front[idx] = 0;
} else {
front[idx] = back[idx];
}
gameNum++;
}
}
System.out.println("게임 종료 ");
scan.close();
}
}