loop문, 순환문, 반복문
int n;
//1.
for( n = 0; n < 2 ; n++ ) {
System.out.println("for loop " + n);
}
//2.
for(int i = 0; i < 10 ; i++) {
System.out.print(i + " ");
}
//3.
for(int i = 10; i > 0 ; i--) {
System.out.print(i + " ");
}
//4.
for(int i = 0; i < 10 ; i=i+2) {
System.out.print(i + " ");
}
//endless loop
/* 무한루프니까 조심!(중간에 범위설정 안하면 무한루프)
for(int i = 0; ; i++) {
System.out.print(i + " ");
}
*/
//5.
// 1 ~ 10 까지 숫자를 더한 합을 구하라
int sum = 0;
for(int i = 1; i <= 10 ; i++) {
sum = sum + i;
}
System.out.println("sum : " + sum);
//6.
// 학생 5명의 국어성적의 합계와 평균
// 90, 85, 95, 100, 85
int numArr[] = new int[6];
numArr[0] = 90;
numArr[1] = 85;
numArr[2] = 95;
numArr[3] = 100;
numArr[4] = 85;
numArr[5] = 75;
sum = 0;
for(int i = 0; i < numArr.length; i++) {
sum += numArr[i];
}
System.out.println("sum : " + sum);
double avg = (double)sum / numArr.length;
System.out.println("avg : " + avg);
//7.
String names[] = { "홍길동", "일지매", "성춘향", "홍두께" };
// 성춘향을 찾아라
// 몇 번째인가
int number = 0;
for(int i = 1; i < names.length; i++) {
number++;
if(names[i].equals("성춘향")) {
System.out.println("찾았습니다");
System.out.println("번호는 " + (i + 1) + "번 입니다.");
}
}
//8.
// 최고 점수를 출력하라.
int max = numArr[0];
for (int i = 1; i < numArr.length; i++) {
if(max < numArr[i]) {
max = numArr[i]; // 0 90 95
}
}
System.out.println("max:" + max);
//9.
// for each
for (int i = 0; i < names.length; i++) {
System.out.println(names[i]);
}
//10.
for (int num : numArr) { // [0] ~ [n-1] for( n in arr)
System.out.print(num + " ");
}
//11.
// for문안에 for문 -> 2중 for문
for(int i = 0; i < 3; i++) {
System.out.println("i = " + i);
for(int j = 0; j < 4; j++) { // i=0일때 j는 0,1,2,3 i=1일때 j=0,1,2,3 ...
System.out.println("\tj = " + j);
}
}
//12.
// 구구단
for(int i=1; i<10; i++) {
for(int j=1; j<10; j++) {
System.out.print(i + " x " + j + " = " + (i*j) + " ");
}
System.out.println(); // 1단이 끝나면 개행
}
//13.
int array2[][] = { // array2[3][4]
{ 11, 12, 13, 14 }, // array2[0].length
{ 21, 22, 23, 24 },
{ 31, 32, 33, 34 },
};
for(int i = 0; i < array2.length; i++) {
for (int j = 0; j < array2[i].length; j++) {
System.out.print(array2[i][j] + " ");
}
System.out.println();
}
===================================================
//1.
for loop 0
for loop 1
//2.
0 1 2 3 4 5 6 7 8 9
//3.
10 9 8 7 6 5 4 3 2 1
//4.
0 2 4 6 8
//5.
sum : 55
//6.
sum : 530
avg : 88.33333333333333
//7.
찾았습니다
번호는 3번 입니다.
//8.
max:100
//9.
홍길동
일지매
성춘향
홍두께
//10.
90 85 95 100 85 75
//11.
i = 0
j = 0
j = 1
j = 2
j = 3
i = 1
j = 0
j = 1
j = 2
j = 3
i = 2
j = 0
j = 1
j = 2
j = 3
//12.
1 x 1 = 1 1 x 2 = 2 1 x 3 = 3 1 x 4 = 4 1 x 5 = 5 1 x 6 = 6 1 x 7 = 7 1 x 8 = 8 1 x 9 = 9
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27
4 x 1 = 4 4 x 2 = 8 4 x 3 = 12 4 x 4 = 16 4 x 5 = 20 4 x 6 = 24 4 x 7 = 28 4 x 8 = 32 4 x 9 = 36
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45
6 x 1 = 6 6 x 2 = 12 6 x 3 = 18 6 x 4 = 24 6 x 5 = 30 6 x 6 = 36 6 x 7 = 42 6 x 8 = 48 6 x 9 = 54
7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63
8 x 1 = 8 8 x 2 = 16 8 x 3 = 24 8 x 4 = 32 8 x 5 = 40 8 x 6 = 48 8 x 7 = 56 8 x 8 = 64 8 x 9 = 72
9 x 1 = 9 9 x 2 = 18 9 x 3 = 27 9 x 4 = 36 9 x 5 = 45 9 x 6 = 54 9 x 7 = 63 9 x 8 = 72 9 x 9 = 81
//13.
11 12 13 14
21 22 23 24
31 32 33 34
'Java' 카테고리의 다른 글
[Java] HashMap (0) | 2022.04.10 |
---|---|
[Java] Generic (0) | 2022.04.10 |
[Java] File (0) | 2022.04.10 |
[Java] Encapsul(캡슐화) (0) | 2022.04.10 |
[Java] Continue (0) | 2022.04.10 |
댓글