back/Java
KH 9일차
devel0per
2022. 7. 12. 15:11
오늘은 Eclipse로 Tool을 바꾸고 배열에 대해서 집중적으로 학습하기 시작했다.
1차원 배열과, 2차원 배열에 대해서 학습했고, 학습한 배열의 개념을 통해서 로또번호를 추출하는것으로 마무리했다.
1) 1차원 배열 선언법
int[] 변수명 = new int[배열크기];
public class Array_1 {
public static void main(String[] args) {
int[] month = new int[12]; // 배열 객체 생성
month[0] = 31;
month[1] = 31;
month[2] = 31;
month[3] = 31;
month[4] = 31;
month[5] = 31;
month[6] = 31;
month[7] = 31;
month[8] = 31;
month[9] = 31;
month[10] = 31;
month[11] = 31;
// month[12] = 31;
for (int i = 0; i < 12; i++) {
System.out.println((i + 1) + " 월" + month[i] + " 일");
}
}
}
int[] 변수명 = new int[] {배열값1, 배열값2, 배열값3, 배열값4};
public class Array_2 {
public static void main(String[] args) {
int[] month = new int[] {31, 28, 31, 30, 31, 30, 31, 31, 31, 31, 31, 30};
for (int i = 0; i < month.length; i++) {
System.out.println((i+1) + "월 :" + month[i] + " 일");
}
}
}
int [] 변수명 = {배열값1, 배열값2, 배열값3, 배열값4};
public interface Array_3 {
public static void main(String[] args) {
int[] month = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (int i = 0; i < month.length; i++) {
System.out.println((i+1) + "월 :" + month[i] + " 일");
}
}
}
2) 2차원 배열 선언법 (성적표 만들기)
package ja_0712;
public class Array_4 {
public static void main(String[] args) {
//4행 3열
// int [][] one = new int[4][3];
//
// one[0][0] = 1;
// one[0][1] = 2;
// one[0][2] = 88;
// one[1][0] = 3;
// one[1][1] = 4;
// one[1][2] = 88;
// one[2][0] = 5;
// one[2][1] = 6;
// one[2][2] = 88;
// one[3][0] = 7;
// one[3][1] = 8;
// one[3][2] = 88;
int [][] one = {{1,2,88},
{3,4,88},
{7,8,88}};
int i, j;
for (i = 0; i < one.length; i++) {
for (j = 0; j < one[i].length; j++) {
System.out.print(" " + one[i][j]);
}
System.out.println();
}
}
}
package ja_0712;
public class Array_6 {
public static void main(String[] args) {
//5행 3열
int[][] score = {{100,100,100,100},
{90,90,90,90},
{80,80,80,80},
{70,70,70,70},
{60,60,60,60}};
int korTotal = 0; // 국어 총점
int engTotal = 0; // 영어 총점
int mathTotal = 0; // 수학 총점
int sciTotal = 0; // 과학 총점
System.out.println(" 번호\t 국어\t영어\t수학\t과학\t총점\t 평균");
System.out.println("=======================================================");
// score.lnegth = 행의 개수 (6)
for (int i = 0; i < score.length; i++) {
int sum = 0;
korTotal += score[i][0]; //[][] 2차원 배열에서 앞의 [] 개념은 행 뒤의 [] 개념은 열
engTotal += score[i][1]; //행은 for문으로 반복하고 열은 고정되어 있기 때문에
mathTotal += score[i][2];
sciTotal += score[i][3];
System.out.print(" " + (i + 1) + " ");
// score[i].length = 열의 갯수 (4)
for (int j = 0; j < score[i].length; j++) {
sum += score[i][j];
System.out.print(score[i][j] + "\t");
}
System.out.println(sum + "\t" + (float)sum / score[i].length);
}
System.out.println("=======================================================");
System.out.println("총점 : " + korTotal + "\t" + engTotal + "\t" + mathTotal + "\t" + sciTotal);
}
}
3) 배열속의 최소값 최대값 찾기
public class Array_8 {
public static void main(String[] args) {
int[] score = {83,64,35,97,58,73};
int max = score[0];
int min = score[0];
for (int i = 1; i < score.length; i++) {
if(score[i] > max) {
max = score[i];
}
if(score[i] < min) {
min = score[i];
}
}
System.out.println("최대 값 :" + max);
System.out.println("최소 값 :" + min);
}
}
4) 로또 만들기
package ja_0712;
public class Lotto_1023 {
public static void main(String[] args) {
int[] ball = new int[45];
// 1번부터 45번까지 대입
for (int i = 0; i < ball.length; i++) {
ball[i] = i+1;
}
int temp = 0;
int j = 0;
for (int i = 0; i < 300; i++) {
j = (int)(Math.random()*45); // 0번 부터 44번까지 출력 ~ 돌려주고 ~
temp = ball[0];
ball[0] = ball[j];
ball[j] = temp;
}
System.out.println();
System.out.println("\n 1023차 로또 당첨번호 \n");
for (int i = 0; i < 6; i++) {
System.out.print(ball[i] + "\t");
}
System.out.println();
System.out.print("\n 보너스 당첨 번호");
System.out.println("\t" + ball[6] + " 번 입니다. \n");
}
}