if 문

if else문

if - else 이용해서 효율적으로 처리 가능

다중 if 문

if 여려개 써서 여러가지 조건을 판별

while 문

조건이 참일 경우 계속 실행

복습문제 (구구단)

package sec06;

import java.util.Scanner;

public class 복습문제 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("출력하고자 하는 구구단 : ");
        int row = in.nextInt();
        {
            int column = 1;
            while (column < 10) {
                System.out.printf("%d * %d = %2d\n", row, column, row * column);
                column++;
            }
            System.out.println();

        }
    }
}

do ~ while 문

do{(반복실행문)};
while (조건식);

  • while 문이랑 다른 점 : while은 조건문이 거짓이면 한번도 실행 안하는데, do while 은 한번은 실행함

for문 (초기식; 조건식; 증감식){ 반복실행문 ; }

초기식은 for문 시작할 때 처음 한번만 실행
초기식 실행한 후에는 조건식을 평가하고, 조건식이 참이면 반복실행문을 실행하고 증감식을 실행
조건식이 거짓이면 for문을 끔

practice3_4

package sec06;

import java.util.Scanner;

public class Practice3_4 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int x, sum = 0;
        do {
            System.out.print("양의 정수를 입력하세요 : ");
            x = in.nextInt();
            if (x % 2 == 0)
                sum += x;

        } while (x > 0);
        System.out.println("입력한 정수 중에서 짝수의 합 : " + sum);

    }
}

practice3_5

package sec06;

import java.util.Scanner;

public class Practice3_5 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("숫자를 입력하세요 : ");
        int sum = 0;
        int x = in.nextInt();
        for (int i = 1; i <= x; i++) {
            sum += i;
        }
        System.out.printf("1부터 %d까지 합은 %d입니다.\n", x, sum);
    }
}

practice3_6

package sec06;

public class Practice3_6 {
    public static void main(String[] args) {
        for (int i = 1; i <= 5; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print("*");

            }
            System.out.println("");
        }
    }
}

practice3_7

package sec06;

public class Practice3_7 {
    public static void main(String[] args) {
        for (int a = 1; a <= 20; a++) {
            for (int b = 1; b <= 20; b++) {
                for (int c = 1; c <= 20; c++) {
                    if (a * a + b * b == c * c) {
                        System.out.printf("%2d, %2d. %2d\n", a, b, c);
                    }
                }
            }
        }
    }
}

practice3_8


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("출력하고자 하는 구구단 : ");
        int row;
        while ((row = in.nextInt()) != 0) {
            int column = 1;
            while (column < 10) {
                System.out.printf("%d * %d = %2d\n", row, column, row * column);
                column++;
            }
            System.out.print("출력하고자 하는 구구단 : ");

        }
    }
}

이렇게도 풀 수 있음

package sec06;

import java.util.Scanner;

public class Practice3_8 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("출력하고자 하는 구구단 : ");
        int a;
        while ((a = in.nextInt()) != 0) {
            for (int b = 1; b < 10; b++) {
                System.out.printf("%d * %d = %2d\n", a, b, a * b);
            }
            System.out.print("출력하고자 하는 구구단 : ");
        }
    }
}

'TIL > [객체지향 프로그래밍] TIL' 카테고리의 다른 글

TIL (22.04.06)  (0) 2022.04.06
TIL (22.04.01)  (0) 2022.04.01
TIL (22.03.23)  (0) 2022.03.23
TIL (22.03.16)  (0) 2022.03.16
TIL 1 (22.03.11)  (0) 2022.03.11