1. 반복문
public class Loop {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 10; i++) {
if(i % 2 != 0) {
sum += i;
}
}
System.out.println("sum = " + sum);
System.out.println("-------------------------------");
int sum2 = 0;
for(int i = 1; i <= 10; i++) {
if(i % 2 == 0) {
sum2 += i;
}
}
System.out.println("sum2 = " + sum2);
System.out.println("-------------------------------");
for(int i = 0; i <= 7; i++) {
for(int j = 0; j <= 7; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("-------------------------------");
for(int i = 0; i <= 7; i++) {
for(int j = i; j <= 7; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("-------------------------------");
for(int i = 7; i >= 0; i--) {
for(int j = i; j <= 7; j++) {
System.out.print("*");
}
System.out.println("");
}
System.out.println("-------------------------------");
int multi = 0;
for(int i = 2; i <= 9; i++) {
for(int j = 1; j <= 9; j++) {
multi = i * j;
System.out.println(i + " * " + j + " = " + multi);
}
}
System.out.println("-------------------------------");
int multi2 = 0;
int i = 2;
while(i <= 9) {
int j = 1;
while(j <= 9) {
multi2 = i * j;
System.out.println(i + " * " + j + " = " + multi2);
j++;
}
i++;
}
}
}
2. 배열
public class Array {
public static void main(String[] args) {
int[] num = {12, 56, 23, 39};
int sum = 0;
for(int i = 0; i < num.length; i++) {
sum += num[i];
}
System.out.println("sum = " + sum);
int[] num2 = new int[] {3, 6, 9, 12, 137};
int sum2 = 0;
for(int j = 0; j < num2.length; j++) {
sum2 += num2[j];
}
System.out.println("sum2 = " + sum2);
int[] num3 = {56, 39, 89};
if(num3[0] > num3[1] && num3[0] > num3[2]) {
int max = num3[0];
System.out.println("max = " + max);
} else if(num3[1] > num3[2]) {
int max = num3[1];
System.out.println("max = " + max);
} else {
int max = num3[2];
System.out.println("max = " + max);
}
int[] num3_1 = {39, 23, 56, 152, 59, 96, 85};
int max2 = num3_1[0];
for(int k = 0; k < num3_1.length; k++) {
if(max2 < num3_1[k]) {
max2 = num3_1[k];
}
}
System.out.println("max = " + max2);
int max3 = 0;
for(int m : num3_1) max3 = Math.max(m, max3);
System.out.println("max = " + max3);
int[] num4 = {39, 23, 56, 59, 96, 85};
int even = 0;
int odd = 0;
for(int n = 0; n < num4.length; n++) {
if(num4[n] % 2 == 0) {
even += num4[n];
} else {
odd += num4[n];
}
}
System.out.println("짝수의 합 = " + even);
System.out.println("홀수의 합 = " + odd);
}
}