[TIL] 배열 예제
1)배열을 출력하되 가로가 아닌 세로순으로 출력하기
행과 열의 위치를 바꿔 출력되는 순서를 변경하여 풀 수 있는 문제였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package ex0730;
public class Array_Test1 {
public static void main(String[] args) {
int[][] a = new int[5][4];
int n = 0;
for (int i = 0; i < a[i].length; i++) {
for (int j = 0; j < a.length; j++) {
n++;
System.out.println(j+":"+i+"="+n);
a[j][i] = n;
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[j].length; j++) {
System.out.printf("%4d", a[i][j]);
}
System.out.println();
}
}
}
/*
0:0=1
1:0=2
2:0=3
3:0=4
4:0=5
0:1=6
1:1=7
2:1=8
3:1=9
4:1=10
0:2=11
1:2=12
2:2=13
3:2=14
4:2=15
0:3=16
1:3=17
2:3=18
3:3=19
4:3=20
1 6 11 16
2 7 12 17
3 8 13 18
4 9 14 19
5 10 15 20
*/
|
cs |
2)배열을 출력하되 역순으로 출력하기
2중 for문을 수정하여 역순으로 배열이 출력되게 만들었다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
package ex0730;
public class Array_Test2 {
public static void main(String[] args) {
int[][] a = new int[5][4];
int n = 0;
for (int i = 4; i >= 0; i--) {
for (int j = 3; j >=0; j--) {
n++;
System.out.println(i + ":" + j + "=" + n);
a[i][j] = n;
/* 출력값
20 19 18 17
16 15 14 13
12 11 10 9
8 7 6 5
4 3 2 1
*/
}
}
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[j].length; j++) {
System.out.printf("%4d", a[i][j]);
}
System.out.println();
}
}
}
|
cs |
3) 배열을 출력하되 짝수행은 역순으로 출력하기
if문을 통하여 2중for문에서 출력되는 값에 변화를 주는 문제였다.
반대로 a[i][j] = n; 이부분에 수치변화를 줘도 되는 문제였다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package ex0730;
public class Array_Test3 {
public static void main(String[] args) {
int[][] a = new int[5][4];
int n = 0;
for (int i = 0; i < a.length; i++) {
if (i % 2 == 1) {
for (int j = 3; j >= 0; j--) {
n++;
System.out.println(i + ":" + j + "=" + n);
a[i][j] = n;
}
} else {
for (int j = 0; j < a[i].length; j++) {
n++;
System.out.println(i + ":" + j + "=" + n);
a[i][j] = n;
}
}
}
/* 출력값
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
17 18 19 20
*/
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[j].length; j++) {
System.out.printf("%4d", a[i][j]);
}
System.out.println();
}
}
}
|
cs |
4) 성적을 입력하면 총점/평균/석차/학점까지 출력되는 프로그램 만들기
2중for문을통하여 점수값을 입력하였고
switch문을 활용하여 점수별 학점을 부여함
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
package ex0730;
import java.util.Scanner;
public class Array_Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cnt;
String[] name;
int[][] score;
int[] tot;
int[] rank;
String[] subject = { "국어", "영어", "수학" };
do {
System.out.print("인원수?");
cnt = sc.nextInt();
} while (cnt < 1);
// 메모리 할당
name = new String[cnt];
score = new int[cnt][3];
tot = new int[cnt];
rank = new int[cnt];
// 데이터 입력
for (int i = 0; i < cnt; i++) {
System.out.print((i + 1) + "번째 이름?");
name[i] = sc.next();
for (int j = 0; j < score[i].length; j++) {
System.out.print(subject[j] + " ?");
score[i][j] = sc.nextInt();
tot[i] += score[i][j];
}
// 석차
rank[i] = 1;
}
for(int s=0;s<cnt-1;s++) {
for(int j= s+1;j<cnt;j++) {
if(tot[s]<tot[j]) {
rank[s]++;
}else if(tot[j]<tot[s]) {
rank[j]++;
}
}
}
//출력
System.out.println("\n--------------------------------");
System.out.println("이름\t국어\t영어\t수학\t총점\t평균\t석차");
System.out.println("\n--------------------------------");
for(int i=0;i<cnt;i++) {
System.out.print(name[i]+"\t");
for(int j=0;j<score[i].length;j++) {
System.out.print(score[i][j]+"\t");
}
System.out.print(tot[i]+"\t");
System.out.print((tot[i]/3)+"\t");
System.out.print(rank[i]+"\n");
String grade;
System.out.print("\t");
for(int s=0;s<cnt;s++) {
switch(score[i][s]/10) {
case 10:case 9:grade ="수";break;
case 8:grade ="우";break;
case 7:grade ="미";break;
case 6:grade ="양";break;
default:grade ="가";break;
}
System.out.print(grade+"\t");
}
System.out.println();
}
sc.close();
}
}
/*
인원수?3
1번째 이름?이영헌
국어 ?78
영어 ?89
수학 ?100
2번째 이름?전상훈
국어 ?89
영어 ?75
수학 ?68
3번째 이름?미솜
국어 ?78
영어 ?68
수학 ?98
--------------------------------
이름 국어 영어 수학 총점 평균 석차
--------------------------------
이영헌 78 89 100 267 89 1
미 우 수
전상훈 89 75 68 232 77 3
우 미 양
미솜 78 68 98 244 81 2
미 양 수
**/
|
cs |
'Language > JAVA' 카테고리의 다른 글
[TIL] 객체지향 프로그래밍 (0) | 2020.07.31 |
---|---|
[TIL] 배열의 복사 (0) | 2020.07.31 |
[TIL] 2차원 배열 (0) | 2020.07.30 |
[TIL] 배열 (2) (0) | 2020.07.30 |
[TIL] 배열 Array (0) | 2020.07.29 |
댓글