본문 바로가기

프로그래밍 공부/Java

Java - 반복문

반복문 역시 JavaScript와 동일하다. for문 while문 do while문을 사용하는 것도 동일하며 JavaScript처럼 별을 찍는 것도, 구구단을 출력하는 것도 동일하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class Main {
    public static void main(String[] args) {
        // while문
        int a = 0;
        while(a <= 5) {
            System.out.println(a);
            a++;
        }
        
        // for문
        for(int b = 0; b <= 5; b++) {
            System.out.println(b);
        }
    };
};
cs

for문과 while문으로 별찍기

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
public class Main {
    public static void main(String[] args) {
        // for문으로 별찍기        
        String star1 = "";
        for(int i = 0; i < 5; i++) {
            for(int j = 0; j < 5-i; j++) {
                star1 += "*";
            }
            star1 += "\n";
        }
        System.out.println(star1);
        
        // while문으로 별찍기
        String star2 = "";
        int k = 0;
        while(k < 5) {
            k++;
            int l = 0;
            while(l < 6-k) {
                l++;
                star2 += "*";
            }
            star2 += "\n";
        }
        System.out.println(star2);    
    };
};
cs

for문과 while문으로 구구단 출력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public class Main {
    public static void main(String[] args) {
        // for문으로 구구단 출력        
        for(int c = 2; c < 10; c++) {
            for(int d = 1; d < 10; d++) {
                System.out.println(c + "*" + d + "=" + (c*d));
            }
            System.out.println("\n");
        }
        
        // while문으로 구구단 출력
        int e = 1;
        while(e < 9) {
            e++;
            int f = 1;
            while(f < 10) {
                System.out.println(e + "*" + f + "=" + (e*f));
                f++;
            }
            System.out.println("\n");
        }
    };
};
cs

do while문도 JavaScript와 동일하다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num;
        int sums = 0;
        
        // do while
        do {
            System.out.print("숫자를 입력하세요. : ");
            num = scanner.nextInt();
            sums += num;
        }  while(num != 0);
        System.out.println("총 합계 = " + sums);
 
        scanner.close();
    };
};
cs

특정 숫자 또는 입력받은 숫자까지 합을 구하는 코드도 JavaScript와 같이 만들 수 있다.

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
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
        // for문으로 1부터 5까지의 합
        int sums1 = 0;
        for(int i = 1; i <= 5; i++) {
            sums1 += i;
        }
        System.out.println(sums1);
        
        // while문으로  1부터 10까지의 합
        int sums2 = 0;
        int j = 1;
        while(j <= 10) {
            sums2 += j;
            j++;
        }
        System.out.println(sums2);
        
        // Scanner로 입력받아 입력 받은 숫자까지의 합을 구하는 반복문
        Scanner scanner = new Scanner(System.in);
        System.out.print("어디까지 더하실 생각입니까? : ");
        int k = scanner.nextInt();
        int sums3 = 0;
        for(int l = 1; l <= k; l++) {
            sums3 += l;
        }
        System.out.println(sums3);
   };
};
cs

 

'프로그래밍 공부 > Java' 카테고리의 다른 글

Java - 반복문으로 별찍기 심화 예제  (0) 2020.01.30
Java - 반복문으로 별찍기 예제  (0) 2020.01.23
Java - 조건문  (0) 2020.01.21
Java - 연산자  (0) 2020.01.21
Java - 출력과 입력  (0) 2020.01.21