package day026;
import java.util.Random;
public class Day02601복습문제 {
public static void main(String[] args) {
// # [14번 게시글]4의 배수만 저장(선행예제)
// # 랜덤 성적
// 1. 1~100 사이의 숫자를 랜덤하게 scores배열에 저장
// 2. 성적이 60점 이상이면, success 배열에 저장
// 3. 성적이 60점 미만이면, fail 배열에 저장
Random ran = new Random();
int[] scores = new int[10];
int[] success = null;
int[] fail = null;
// 합격생의 수 저장
int successCount = 0;
// 불합격생의 수 저장
int failCount = 0;
// 랜덤하게 10개의 성적 저장 및 출력
for(int i=0; i<10; i++) {
scores[i] = ran.nextInt(100) + 1;
System.out.print(scores[i] + " ");
}
System.out.println();
// 합격생 수와 불합격생의 수 구하기
for(int i=0; i<10; i++) {
if(scores[i] >= 60) {
successCount += 1;
}
}
failCount = 10 - successCount;
success = new int[successCount];
fail = new int[failCount];
// success와 fail배열에 각각의 성적 저장하기
int successIdx = 0;
int failIdx = 0;
for(int i=0; i<10; i++) {
if(scores[i] >= 60) {
success[successIdx] = scores[i];
successIdx += 1;
}else {
fail[failIdx] = scores[i];
failIdx += 1;
}
}
// success와 fail배열 출력하기
System.out.print("합격생 = [ ");
for(int i=0; i<successCount; i++) {
System.out.print(success[i] + " ");
}
System.out.println("]");
System.out.print("불합격생 = [ ");
for(int i=0; i<failCount; i++) {
System.out.print(fail[i] + " ");
}
System.out.println("]");
}
}
package day026;
import java.util.Scanner;
public class Day02602배열응용 {
public static void main(String[] args) {
// 1. 숫자 5개를 입력받는다.
// 2. 입력한 값이 arr배열 안에 존재하는 값이면
// temp 배열 안에 해당 값의 인덱스를 차례대로 저장한다.
// 3. 단, 입력한 숫자가 arr 배열에 없으면 인덱스 대신 -1을 저장한다.
// 예) 입력 : 10, 20, 10, 1, 50
// temp = {0, 1, 0, -1, 4}
// 예) 입력 : 30, 40, 1, 10, 2
// temp = {2, 3, -1, 0, -1}
Scanner scan = new Scanner(System.in);
int[] arr = {10, 20, 30, 40, 50};
int[] temp = new int[5];
// # 방법1)
for(int i=0; i<5; i++) {
System.out.println("숫자 입력");
int num = scan.nextInt();
temp[i] = -1;
for(int j=0; j<5; j++) {
if(num == arr[j]) {
temp[i] = j;
}
}
}
// # 방법2)
// for(int i=0; i<5; i++) {
// System.out.println("숫자 입력");
// int num = scan.nextInt();
//
// int check = -1; // num이 arr에 없을 때
// for(int j=0; j<5; j++) {
// if(num == arr[j]) {
// check = j;
// }
// }
//
// if(check == -1) {
// temp[i] = -1;
// }else {
// temp[i] = check;
// }
// }
// 출력
for(int i=0; i<5; i++) {
System.out.print(temp[i] + " ");
}
}
}
package day026;
import java.util.Scanner;
public class Day02603최대값구하기 {
public static void main(String[] args) {
/*
* # 최대값 구하기
* 1. 가장 큰 값을 찾아 입력한다.
* 2. 정답이면 해당 값을 0으로 변경한다.
* 3. arr 배열의 모든 값이 0으로 변경되면 프로그램은 종료된다.
*
* 11, 87, 42, 100, 24
* 입력 : 100
*
* 11, 87, 42, 0, 24
*
* 입력 : 87
* 11, 0, 42, 0, 24
*/
Scanner scan = new Scanner(System.in);
// for문으로 변경해서 풀어보시오!
int[] arr = {11, 87, 42, 100, 24};
int i = 0;
while(i < 5) {
for(int j=0; j<5; j++) {
System.out.print(arr[j] + " ");
}
System.out.println();
System.out.println("최대값 입력");
int myNum = scan.nextInt();
int maxIdx = 0;
int maxNum = 0;
for(int j=0; j<5; j++) {
if(maxNum < arr[j]) {
maxNum = arr[j];
maxIdx = j;
}
}
if(myNum == maxNum) {
arr[maxIdx] = 0;
i = i + 1;
}
}
}
}
package day026;
import java.util.Scanner;
public class Day02604숫자이동2 {
public static void main(String[] args) {
/*
* # 숫자이동[2단계]
* 1. 숫자 2는 캐릭터이다.
* 2. 숫자 1을 입력하면, 캐릭터가 왼쪽으로
* 숫자 2을 입력하면, 캐릭터가 오른쪽으로 이동한다.
* 3. 단, 좌우 끝에 도달했을 때, 예외처리를 해야한다.
* 4. 숫자 1은 벽이다. 벽을 만나면 이동할 수 없다.
* 5. 단, 숫자 3을 입력하면, 벽을 격파할 수 있다.
*/
Scanner scan = new Scanner(System.in);
int[] game = {0, 0, 1, 0, 2, 0, 0, 1, 0};
// 캐릭터(2)의 위치를 저장
int player = 0;
for(int i=0; i<9; i++) {
if(game[i] == 2) {
player = i;
}
}
// System.out.println(player);
// 게임시작
while(true) {
// 게임화면 출력
for(int i=0; i<9; i++) {
if(game[i] == 2) {
System.out.print("옷 ");
}else if(game[i] == 1) {
System.out.print("벽");
}else {
System.out.print("__");
}
}
System.out.println();
// 방향 입력받기
System.out.println("1)left 2)right");
int move = scan.nextInt();
if(move == 1) {
// 끝에 도달했을 때의 예외처리
if(player == 0) {
continue;
}
// 벽을 만나면
if(game[player - 1] == 1) {
System.out.println("3)punch");
int punch = scan.nextInt();
if(punch != 3) {
continue;
}
}
game[player] = 0;
player -= 1; // player = player - 1;
game[player] = 2;
}
else if(move == 2) {}
}
}
}