컴퓨터프로그래밍및실습 (2022년)/0915

From DISLab
Jump to navigation Jump to search

조건문

이 부분의 본문은 컴퓨터프로그래밍및실습 (2022년)/조건문입니다.

if 문

public class IfExample {
    public static void main(String[] args) {
        int score = 93;

        //-----------------------------------------
        // if 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        }

        if (score < 90) {
            System.out.println("B");
        }

        //-----------------------------------------
        // if-else 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        } else {
            System.out.println("B");
        }

        //-----------------------------------------
        // if-else if-else 문
        //-----------------------------------------

        if (score >= 90) {
            System.out.println("A");
        } else if (score >= 80) {
            System.out.println("B");
        } else if (score >= 70) {
            System.out.println("C");
        } else if (score >= 60) {
            System.out.println("D");
        } else {
            System.out.println("F");
        }

        //-----------------------------------------
        // 중첩 if 문
        //-----------------------------------------

        if (score >= 90) {
            if (score >= 95) {
                System.out.println("A+");
            } else {
                System.out.println("A0");
            }
        } else {
            if (score >= 85) {
                System.out.println("B+");
            } else {
                System.out.println("B0");
            }
        }
    }
}


switch 문

public class SwitchExample {
    public static void main(String[] args) {
        int num = (int) (Math.random() * 10); // 0 ~ 9

        switch(num) {
            case 1 :
                System.out.println("1");
                break;
            case 2 :
                System.out.println("2");
                break;
            case 3 :
                System.out.println("3");
            case 4 :
                System.out.println("4");
                break;
            case 5 :
            case 6 :
            case 7 :
                System.out.println("789");
                break;
            default :
                System.out.println("OOPS");
                break; // break가 필요한가?
        }

        //----------------------------
        //  문자
        //----------------------------

        char grade = (char)('A' + num);

        switch(grade) {
            case 'A' :
                System.out.println("A");
                break;
            case 'B' :
                System.out.println("B");
                break;
            case 'C' :
                System.out.println("C");
                break;
            case 'D' :
                System.out.println("D");
                break;
            default:
                System.out.println("F");
                break;
        }

        //----------------------------
        //  String (JDK 7부터)
        //----------------------------

        String str = "과장";

        switch(str) {
            case "부장" :
                System.out.println("700만원");
                break;
            case "과장" :
                System.out.println("500만원");
                break;
            default:
                System.out.println("300만원");
        }
    }
}


반복문

이 부분의 본문은 컴퓨터프로그래밍및실습 (2022년)/반복문입니다.

for 문

  • 1 ~ 100까지 더하는 연산
int sum = 0;

for(int i = 1; i <= 100; i++) {
    sum += i;
}
System.out.println("1~" + (i - 1) + 까지의  : " + sum);
for ( int i = 0, j = 100; i <= 50 && j >= 50; i++, j-- ) {
초기화식 조건식 증감식

while 문

  • 1 ~ 100까지 더하는 연산
int sum = 0;

int i = 1;
while (i <= 100) {
    sum += i;
    i++;
}
System.out.println("1~" + (i - 1) + 까지의  : " + sum);

do-while 문

  • 1 ~ 100까지 더하는 연산
int sum = 0;

int i = 1;
do {
    sum += i;
    i++;
} while (i <= 100);
System.out.println("1~" + (i - 1) + 까지의  : " + sum);
  • System.in 객체를 이용하여 키보드로부터 값을 읽어들이는 방법
int keyCode = System.in.read();

if (keyCode == 49) { // 1을 읽었을 경우
  • Key Code
키코드 키코드 키코드 키코드 키코드 키코드 키코드
0 48 A 65 N 78 a 97 n 110 Backspace 8 37
1 49 B 66 O 79 b 98 o 111 Tab 9 38
2 50 C 67 P 80 c 99 p 112 Enter CR=13, LF=10 39
3 51 D 68 Q 81 d 100 q 113 Shift 16 40
4 52 E 69 R 82 e 101 r 114 Ctrl 17
5 53 F 70 S 83 f 102 s 115 Alt 18
6 54 G 71 T 84 g 103 t 116 ESC 27
7 55 H 72 U 85 h 104 u 117 Space 32
8 56 I 73 V 86 i 105 v 118 PAGEUP 33
9 57 J 74 W 87 j 106 w 119 PAGEDOWN 34
K 75 X 88 k 107 x 120
L 76 Y 89 l 108 y 121
M 77 Z 90 m 109 z 122

  • Scanner 객체를 이용하여 값을 읽어들이는 방법
import java.util.Scanner;

public class ScannerExample {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int v1 = scan.nextInt();
        double v2 = scan.nextDouble();
        boolean v3 = scan.nextBoolean();
        
        String str = scan.next();
        String line = scan.nextLine();
    }
}


break 문

  • 자신이 속한 반복문 탈출
int sum = 0;

int i = 1;
while (true) {
    sum += i;
    if (i == 0)
        break; // 반복문 탈출
}
System.out.println(sum);
  • 지정한 반복문 탈출
Outer:
for (char upper = 'A'; upper <= 'Z'; upper++) {
    for(char lower = 'a'; lower <= 'z'; lower++) {
        System.out.println(upper + "-" + lower);
        if (lower == 'g')
            break Outer;
    }
}
A-a
A-b
A-c
A-d
A-e
A-f
A-g

continue 문

  • continue 문을 이용하여 짝수만 출력
for(int i = 1; i <= 10; i++) {
    if (i % 2 != 0)  // 짝수가 아니라면, 즉 홀수
        continue;
    System.out.println(i);
}
  • 지정한 반복문으로 점프
Outer:
for (char upper = 'A'; upper <= 'C'; upper++) {
    for(char lower = 'a'; lower <= 'z'; lower++) {
        System.out.println(upper + "-" + lower);
        if (lower == 'g')
            continue Outer;
    }
}
A-a
A-b
A-c
A-d
A-e
A-f
A-g
B-a
B-b
B-c
B-d
B-e
B-f
B-g
C-a
C-b
C-c
C-d
C-e
C-f
C-g