Java 20221020

신래은·2022년 12월 13일

JAVA

목록 보기
4/22

DAY 4

UpperCase : 대문자
LowerCase : 소문자
IgnoreCase : 대문자&소문자 구분 안함
(if 문에서 사용 가능)

while문은 주로 무한루프 시 이용 (while(true)이용)
나머지는 for문 사용

Code

LoopEx

public class LoopEx2 {
    public static void main(String[] args) {
        for(int i=0; i<10; i++) {
            System.out.println("반복합니다");
            if(i == 5)
                break;
        }
        int i = 0;
        int sum = 0;
        // while(true) {   // 의도적으로 무한히 반복.
        for( ; ; ) {   // for문을 통한 무한반복.
            sum = sum + i;
            if(sum >= 1000)
                break;
            i++;
        }
        System.out.println("반복 횟수 : " + i + " / 총합 : " + sum);
    }
}

InfiniteLoopEx

import java.util.Scanner;

public class InfiniteLoop {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        
        // 0을 선택하기 전까지 무한반복.
        while(true) {
            int sel = 0;
            System.out.println("====================");
            System.out.println("1. 회원추가");
            System.out.println("2. 회원조회");
            System.out.println("3. 회원수정");
            System.out.println("4. 회원삭제");
            System.out.println("0. 종료");
            System.out.print("선택 : ");
            sel = s.nextInt();
        
            if(sel == 0) break;
            else if(sel == 1) System.out.println("회원 추가 기능 실행");
            else if(sel == 2) System.out.println("회원 조회 기능 실행");
            else if(sel == 3) System.out.println("회원 수정 기능 실행");
            else if(sel == 4) System.out.println("회원 삭제 기능 실행");
            else System.out.println("잘못된 기능 번호 입니다.");
        }
        s.close(); //무한반복에서 break를 걸지 않으면 Daed code 발생.
    }
}

ContinueEx

public class LoopEx3 {
    public static void main(String[] args) {
        for(int i=0; i<10; i++) {
            System.out.println("반복합니다." + i);
            if(i % 3 == 0)
                continue;   // 조건에 해당할 시 for문으로 다시 돌아감. 아래 명령 무시.
                            // filtering 역할
            System.out.println("continue 다음 출력");
        }
    }
}

UpAndDownGameEx

import java.util.Scanner;

public class UpAndDown {
    public static void main(String[] args) {
        // 1~100 사이의 임의의 값을 얻어서 answer에 저장한다.
        int answer = (int)(Math.random()*100+1)/* (1) */ ;
        int input = -1;   // 사용자입력을 저장할 공간
        int count = 0;   // 시도횟수를 세기위한 변수
        
        // Scanner 화면으로 부터 사용자입력을 받기 위해서 클래스 사용
        Scanner s = new Scanner(System.in);
        
        while (true) {
            count++;
            System.out.print("1과 100사이의 값을 입력하세요 : ");
            input = s.nextInt(); // input입력받은 값을 변수에 저장한다.
            if(input < answer) {
                System.out.println("Up");
            }
            else if(input > answer) {
                System.out.println("Down");
            }
            else {
                System.out.println("정답입니다.");
                System.out.println("시도 횟수 : " + count + "회");
                break;
            }
        } 
        s.close();
    }
}

ArrayEx

public class ArrayEx01 {
    public static void main(String[] args) {
        int[] arr = new int[3]; // 생성(선언)
        
        for(int i=0; i<3; i++) {
        arr[i] = (i+1)*10;
        System.out.println(arr[i]);
        }
       
        // arr = new int[5];    // 덮어쓰기 (정적 배열 - static array)
        // for(int i=0; i<5; i++) {
        // arr[i] = (i+1)*10;
        // System.out.println(arr[i]);
        // }
    }   
}

ArrayEx2

public class ArrayEx04 {
    public static void main(String[] args) {
        int[] arr1 = {10, 20, 30};
        int[] arr2 = new int[3];
        System.out.println(arr1.length);    
        //array의 길이를 확인 한 다음, 복사할 때 응용가능.

        // 원본, 원본의 시작위치, 사본, 사본의 시작위치, 복사할 개수)
        // 배열의 완전한 복사 (deep copy) <-> (shallow copy)
        System.arraycopy(arr1, 0, arr2, 0, 3);  
        
        System.out.println("Before");
        for(int i=0; i<3; i++) {
            System.out.println("arr1[" + i + "] : " + arr1[i] + " arr2[" + i + "] : " + arr2[i]);
        }
        arr1[0] = 100;
        System.out.println("After");
        for(int i=0; i<3; i++) {
            System.out.println("arr1[" + i + "] : " + arr1[i] + " arr2[" + i + "] : " + arr2[i]);
        }
    }
}

0개의 댓글