Java 20221021

신래은·2022년 12월 13일

JAVA

목록 보기
5/22

DAY 5

4일간 배운 내용을 정리하는 시간을 가짐.

for (초기화;조건식;증감식) {
조건식이 true일 때, 실행될 문장
}

•for 문은 배열조회를 위해 많이 쓰이고, i의 증감식을 건드리는 경우는 거의 없음.

•데이터를 걸러내기 위해서는 가져올 때 걸러내고 가져올 것.

동적메모리 : heap (array변수(reference type))
정적메모리 : stack (배열의 실제 데이터)

•framework : 복잡한 연산을 해결하게 해주는 프로그램

Code

ConditionEx

import java.util.Scanner;

public class ConditionCheck02 {
    public static void main(String[] args) {
        System.out.print("1.추가 2.수정 3.조회 4.삭제 : ");
        
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
      // if문으로 구현 
        if(n == 1 ) {System.out.println("추가 기능 실행");}
        else if(n == 2 ) {System.out.println("수정 기능 실행");}
        else if(n == 3 ) {System.out.println("조회 기능 실행");}
        else if(n == 4 ) {System.out.println("삭제 기능 실행");}
        else {System.out.println("1-4 중 하나를 선택하세요.");}
      
      // switch문으로 구현
        switch(n) {
            case 1: 
                System.out.println("추가 기능 실행");
                break;
            case 2: 
                System.out.println("수정 기능 실행");
                break;
            case 3: 
                System.out.println("조회 기능 실행");
                break;
            case 4:
                System.out.println("삭제 기능 실행");
                break;
            default:
                System.out.println("1-4 중 하나를 선택하세요.");
        }
        s.close();
    }
}

ArrayLoopEx

public class LoopEx {
    public static void main(String[] args) {
        // 배열(Array)
        int[] arr = new int[5];
        for (int i=0; i<5; i++) {
            arr[i] = (i+1)*10; }
        // 배열 출력
        for(int i=0; i<5; i++) {
            System.out.println(arr[i]); }
            
        int[] arr2 = {5, 10, 15, 20, 25, 30, 35, 40};
        for(int i=0; i<arr2.length ; i++) {
            System.out.println(arr2[i]); }
        int[] arr3 = {10, 20, 30};
        int[] arr4 = arr3;
        
        System.out.println(arr3[0]);
        System.out.println(arr4[0]);
        
        // 결과값 비교
        arr3[0] = 100;
        System.out.println(arr3[0]);
        System.out.println(arr4[0]);
    }
}

for문과 while문

public class LoopEx {
    public static void main(String[] args) {
        for (int i=0; i<5; i++) {
            if(i%2 == 1)                    // 증감식은 건드리지 않음. 들어온 데이터는 다 검사해 보는게 좋음.
            System.out.println(i);          // 보지 않을 데이터라면 애초에 데이터를 가져올 때 걸러내기.
        }

        int i=0;
        while (i<5) {
            if(i%2 == 1) 
            System.out.println(i);
            i++;
        }
   }
}

StringEx

public class Exercise01 {
    public static void main(String[] args) {
        String regNo = "881120-1068234";
        System.out.println(regNo.substring(0, 6));
        System.out.println(regNo.substring(7));
        System.out.println(regNo.split("-")[0]); // 두개로 나뉜 부분 중 0번째 출력.
        System.out.println(regNo.split("-")[1]);
        System.out.println(regNo.charAt(7) == 1 ? "남"  : "여");
        int a = regNo.charAt(7);
        System.out.println(a);                   
        System.out.println(regNo.charAt(7) % 2 == 1 ? "남" : "여");
        
    }
}

0개의 댓글