- ch++ : char ch = (char)(ch + 1)
- 복합 대입 연산자는 강제 형 변환을 하지 않아도 자동적으로 형 변환을 진행한다.
public class NthAlphabet {
public static void main(String[] args) {
int num = 1;
char result = 'A';
for (char ch = 'A'; ch <= 'Z'; ch++) {
if (num == 10) {
result = ch;
break;
}
num++;
}
System.out.println(num + "번째 알파벳 : " + result);
}
}
[Console]
10번째 알파벳 : J
public class NthAlphabetRe {
public static void main(String[] args) {
int num = 1;
char result = 'A';
int count = 22;
boolean search = false;
for (char ch = 'A'; ch <= 'Z'; ch++) {
if (num == count) {
result = ch;
search = true;
break;
}
num++;
}
if(search) {
System.out.println(num + "번째 알파벳 : " + result);
}else {
System.out.println("찾는 알파벳이 없습니다");
}
}
}
[Console]
22번째 알파벳 : V (있을 때)
찾는 알파벳이 없습니다 (없을 때)
public class ContinueTest {
public static void main(String[] args) {
int dan = 9;
for (int i = 1; i <= 9; i++) {
if ((i % 2) != 0) { // 홀수이면
continue;
// 밑으로 내려가지 말고 다음 것을 진행
}
System.out.println(dan + " * " + i + " = " + (dan * i));
}
}
}
[Console]
9 x 2 = 18
9 x 4 = 36
9 x 6 = 54
9 x 8 = 72
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
if ((i % 2) == 1) {
continue;
}
sum += i;
}
System.out.println("1~10까지 짝수들의 합 : " + sum);
}
[Console]
1~10까지 짝수들의 합 : 30
while(true) {
System.out.println("무한 루프");
}
for (;;) {
System.out.println("무한 루프");
}
do {
System.out.println("무한 루프");
} while(true)
public class ForInFor {
public static void main(String[] args) {
for (int i = 0; i < 3; i++) {
System.out.println("--------------------");
for (int j = 0; j < 3; j++) {
System.out.print("[" + i + ", " + j + "]");
}
System.out.print("\n");
}
}
}
public class Gugudan {
public static void main(String[] args) {
for (int i = 2; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + " x " + j + " = " + (i * j));
}
System.out.println();
}
}
}
[Console]
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
public class Gugudan2 {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 2; j <= 9; j++) {
System.out.print(j + " * " + i + " = " + (i * j) + "\t");
}
System.out.println();
}
}
}
[Console]
- if문 사용 (짝수일때만 조건이 true이도록)
public class GugudanEven {
public static void main(String[] args) {
for (int i = 2; i <= 9; i++) {
if((i%2)==0) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + " x " + j + " = " + (i * j));
}
}
System.out.println();
}
}
}
- continue문 사용 (홀수 조건에 continue 건다)
public class JavaPractice {
public static void main(String[] args) {
for (int i = 2; i <= 9; i++) {
if ((i % 2) == 1)
continue;
for (int j = 1; j <= 9; j++) {
System.out.println(i + " x " + j + " = " + (i * j));
}
System.out.println();
}
}
}
[Console]
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
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
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
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
public class GugudanSum {
public static void main(String[] args) {
int num = 0;
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
System.out.println(i + " x " + j + " = " + (i * j));
num += (i * j);
}
System.out.println();
}
System.out.println("구구단 총합 : " + num);
}
}
[Console]
구구단 총합 : 2025
public class GugudanReverse {
public static void main(String[] args) {
int num = 0;
for (int i = 9; i >= 2; i--) {
for (int j = 9; j >=1; j--) {
System.out.println(i + " x " + j + " = " + (i * j));
}
System.out.println();
}
}
}
[Console]
9 x 9 = 81
9 x 8 = 72
9 x 7 = 63
9 x 6 = 54
9 x 5 = 45
9 x 4 = 36
9 x 3 = 27
9 x 2 = 18
9 x 1 = 9
8 x 9 = 72
8 x 8 = 64
8 x 7 = 56
8 x 6 = 48
8 x 5 = 40
8 x 4 = 32
8 x 3 = 24
8 x 2 = 16
8 x 1 = 8
7 x 9 = 63
7 x 8 = 56
7 x 7 = 49
7 x 6 = 42
7 x 5 = 35
7 x 4 = 28
7 x 3 = 21
7 x 2 = 14
7 x 1 = 7
6 x 9 = 54
6 x 8 = 48
6 x 7 = 42
6 x 6 = 36
6 x 5 = 30
6 x 4 = 24
6 x 3 = 18
6 x 2 = 12
6 x 1 = 6
5 x 9 = 45
5 x 8 = 40
5 x 7 = 35
5 x 6 = 30
5 x 5 = 25
5 x 4 = 20
5 x 3 = 15
5 x 2 = 10
5 x 1 = 5
4 x 9 = 36
4 x 8 = 32
4 x 7 = 28
4 x 6 = 24
4 x 5 = 20
4 x 4 = 16
4 x 3 = 12
4 x 2 = 8
4 x 1 = 4
3 x 9 = 27
3 x 8 = 24
3 x 7 = 21
3 x 6 = 18
3 x 5 = 15
3 x 4 = 12
3 x 3 = 9
3 x 2 = 6
3 x 1 = 3
2 x 9 = 18
2 x 8 = 16
2 x 7 = 14
2 x 6 = 12
2 x 5 = 10
2 x 4 = 8
2 x 3 = 6
2 x 2 = 4
2 x 1 = 2
public class WhileGugudan {
public static void main(String[] args) {
int a = 1;
while (a <= 9) {
int b = 1;
while (b <= 9) {
System.out.println(a + " x " + b + " = " + (a * b));
b++;
}
System.out.println();
a++;
}
}
}
[Console]
public class JavaPractice {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print('*');
}
System.out.println();
}
}
}