Java 20221018

신래은·2022년 12월 13일

JAVA

목록 보기
2/22

DAY 2

Variable

long 타입 뒤에는 리터럴 L 입력
char 타입은 ''입력
string 타입은 "" 입력

; 잊어버리지 말기

단축키 *multi line copy & paste

ctrl+alt: multi line select
ctrl+좌우 방향키: 한단어씩 이동
shift+방향키 : select
ctrl+c (multi line copy)
ctrl+alt: multi line select
ctrl+좌우 방향키: 한단어씩 이동
shift+방향키 : select
ctrl+v (multi line paste)

단축키

shift+방향키: 열 선택
ctrl+/ : 주석처리 (잠시 작동하지 않도록 할 때도 이용)
alt+방향키: 줄바꿈(커서가 있는 줄이 이동)
ctrl+마우스 클릭 : 상세 설명 (reference는 가능)

명명규칙

camel case : 단어의 앞 글자 대문자
snake case : 언더스코어로 단어 구분

'_', '$' 기호 사용가능
한글, 한자 사용가능(비추)
띄어쓰기 사용불가
한번 쓴 변수명 두번 사용 불가능 (중복생성 불가능)

type casting(형변환)

연산자
= : 왼쪽항을 오른쪽에 대입 (연산순위 맨 마지막)
== : 같은지 판단

Scanner : final class (new생성가능)
Calendar : abstract class (new 생성불가능 -> getInstance 사용)

Excel : =IF(C2>=18, "성인", "미성년")
JAVA : (age>=18?"성인":"미성년")

if : 제어문 ( ; 이 붙지않음) (항상 명령문이 한개 이상은 존재해야 함)
명령문 ( ; 필요)

Code

Age

import java.util.Scanner;

public class Age {
    public static void main(String[] args) {
        final int CURRENT_YEAR = 2022;
        Scanner s = new Scanner(System.in);
            System.out.println("출생년도를 입력하세요 : ");
            int birthyear = s.nextInt();
            // System.out.print("만 ");
            // System.out.print((20221018-birth)/10000);
            // System.out.print("세 입니다.");
            // 달력 객체 (현재 시각 세팅)
            // Calendar c = Calendar.getInstance();
            // 연도를 가지고 와서, 입력한 값과 계산
            // System.out.println(c.get(Calendar.YEAR) - Year);
            // System.out.print(CURRENT_YEAR - birthyear);
           System.out.println("만 " +(CURRENT_YEAR - birthyear)+ "세 입니다");
            // 스캐너 사용종료
            s.close();
    }
}

ConditionCheck

mport java.util.Scanner;

public class ConditionCheck {
    public static void main(String[] args) {
        // 비교연산자
        // 대소비교 (이상, 이하, 초과, 미만)
        // 동일비교 (같다, 다르다)

        int x = 10;
        System.out.println(x >= 10); // x가 10 이상인가?
        System.out.println(x <= 10); // x가 10 이하인가?
        System.out.println(x > 10); // x가 10 초과인가?
        System.out.println(x < 10); // x가 10 미만인가?

        System.out.println(x == 10); // x가 10인가?
        System.out.println(x != 10); // x가 10이 아닌가?

        Scanner s = new Scanner(System.in);
        // int sel = s.nextInt();
        // System.out.println("1번 선택 : "+(sel == 1));
        // System.out.println("2번 선택 : "+(sel == 2));
        // System.out.println("3번 선택 : "+(sel == 3));
        // System.out.println("4번 선택 : "+(sel == 4));
        // if(sel == 1) System.out.println("1번을 선택함");
        // if(sel == 2) System.out.println("2번을 선택함");
        // if(sel == 3) System.out.println("3번을 선택함");
        // if(sel == 4) System.out.println("4번을 선택함");

        String input = s.nextLine(); //문자열은 nextLine();, Int는 nextInt();, char는 (char)System.in.read();
        if(input.equals("Hello")) System.out.println("Hello World"); // 동일비교 시, ==가 아닌 equals 사용
        if(input.equals("Bye")) System.out.println("Exit Program");

        s.close();

        // System.out.println("출생 연도를 입력하세요 : ");
        // int birthYear = s.nextInt();
        // int age = 2022 - birthYear;
        // // System.out.println("성인 입니까? " +(age >= 18));
        // System.out.println(age >= 18?"성인입니다":"미성년입니다");
        // // String msg = age >= 18?"성인입니다":"미성년입니다";
        // // System.out.println(msg); / 같은 결과 출력
        // s.close();

        int totalCount = 12;
        final int POST_PER_PAGE = 10;
        // totalCount를 POST_PER_PAGE로 나눈 나머지 값이 0을 초과한다면
        // additionalPage의 값을 1로 설정하고, 아니라면 0으로 설정한다.
        int additionalPage = totalCount % POST_PER_PAGE > 0 ? 1 : 0;
        int totalPage = totalCount/POST_PER_PAGE + additionalPage;
        // int totalPage = totalCount/POST_PER_PAGE + (totalCount % POST_PER_PAGE > 0 ? 1 : 0); 로 합칠 수 있음
        System.out.println("총 페이지 수 : "+totalPage);
    }    
}

ScannerEx

import java.util.Scanner;

public class InputEx {
    public static void main(String[] args) {
        // 표준 입력을 대상으로 하는 스캐너 생성
        Scanner s = new Scanner(System.in);
        int input = s.nextInt(); // int 형태의 값 하나 입력 받기
        System.out.println("입력한 값");
        System.out.println(input);
        // 스캐너 사용 종료
        s.close();
    }
}

OperatorEx

public class OperatorEx {
    public static void main(String[] args) {
        System.out.println(10+20);
        System.out.println(10-20);
        System.out.println(10*20);
        System.out.println(10/20); // int / int = int
        System.out.println(10/20.0); // int / double = double / double = double
        System.out.println(10%3); // remain - 10을 3으로 나누었을 때의 나머지 값
        // 몫 : 3 / 나머지 : 1
        System.out.println(10%4);
        // 글의 수 123개, 페이지 당 10개 노출
        // 총 페이지 수는? 13페이지
        // 123 / 10 -> 10
        // 123 / 10.0 -> 12.3
        // If 123 % 10 > 0 페이지 하나 더 추가하게 설정 가능
        
        int value = 100;
        System.out.println(value);
        System.out.println(value+50); // (value = value+50)면 value값 변화
        System.out.println(value);
        // 여기에서의 value의 값은? 100
        int value2 = 20;
        System.out.println(value + value2);
        System.out.println(value - value2);
        System.out.println(value * value2);
        System.out.println(value / value2);
    
        System.out.println("Hello "+"World!");
        String ext = ".jpg";
        System.out.println("img"+ext);
        System.out.println("profile"+1+ext);
        // "profile1"+".jpg"
        // "profile.jpg"
        System.out.println("img"+1+3+ext);
        // "profile1"+3 + "profile13"
        // "profile13"
        System.out.println("img"+(1+3)+ext);
        // "profile4"
        
    }
}

TypecastingEx

public class TypeCasting {
    public static void main(String[] args) {
        byte a = 10; // 묵시적 형변환 (int를 byte로)
        int b = a; // byte를 int로 형변환
        // short c = b; 불가능 (큰 타입에서 작은 타입으로 옮기기 불가)
        short c = (short)b; //명시적 형변환
        System.out.println(a);
        System.out.println(b);
        System.out.println(c);

        double d1 =10.99;
        System.out.println(d1);
        // int i1 = d1; 불가능
        // int i1 = (int)d1;
        // System.out.println(i1); // 소수점 아래 버림
        int i1 = (int)(d1*10);
        d1 = i1 / 10.0;
        System.out.println(d1); //소수점 아래 첫째 자리까지 표시

        int i2 = (int)10000000000L;
        System.out.println(i2); //오버플로우로 값이 바뀜

        char ch = 'A';
        System.out.println((int)ch);
        }
}

0개의 댓글