본문 바로가기
교육/Java&Spring

kosta 클라우드 네이티브 애플리케이션 개발 과정 day 3

by Renechoi 2022. 12. 22.

kosta 클라우드 네이티브 애플리케이션 개발 과정 day 3

 


 

Scanner를 통해 문자열을 입력받아보자. 

 

 

순서상으로 

nextInt(); 

를 먼저하고 

nextLine()을 할 경우 문제가 발생한다. 

 

nextInt는 숫자 입력 후 함께 누른 엔터를 처리하지 않기 때문이다. 

 

 

 


연산자 (operator)

피연산자 (operand) 

 

 

 

사칙 연산자의 자동 타입 변환 : 데이터 타입은 연산을 하면 큰 쪽으로 간다 

100 + 200L => 결과는 Long 타입의 300 

3.0 - 2 => 결과는 double 타입의 1.0

10.0f / 2L => 결과는 float 타입의 5.0

 

 

문자열 + 비문자열 or 비문자열 + 문자열 

=> 문자열이 아닌 데이터를 문자열로 바꾼 후에 연결 

 

365 + "일" 

-> "365일"

 

 


복합대입연산자 -> number +=1;

증감자 -> number++;

 

 

 

int x = 10;
int y = 0; 

y = x++;

x, y는? 

 

x는 무조건 1이 증가한다. 

 

하지만 y는? 후치증감을 한 x를 받기 때문에 

대입연산자와 후치증감자가 있는 상황에서 

대입을 하고 나서 x가 증가가 되기 때문에 

y는 10이 됨. 

 

 

전치 연산과 후치 연산의 차이 

 

 


논리 연산자 

 

|

if (x==y | x==z) {}

-> or 연산자 

-> 두 값이 모두 false면 false

-> 두 값중 하나만 true여도 true 

 

 

if (x==y ^ x==z) {}

-> 하나가 true, 하나가 false면 true / 그렇지 않으면 false 

 

* 피연산자가 boolean 타입이 아닌 정수 타입이면 비트 연산을 수행한다. 

 

 

 

 

숏커트 현상 

&&나 || 연산에서 

첫번째 연산에서 결과값이 결정되는 경우 더 이상 하지 연산을 하지 않는 것 

 

 

 

 

삼항 연산자 

 

private static void ternaryOperator(){
        int a  = 10;
        int b = 20;
        int c = 30;
        
        int test = 0;
        test = a < b ? a- b : b -c;
    }

 

 


5자리수에 대해서 짝수와 홀수의 개수를 구해보자 

 

삼항연산자와 나머지 연산을 사용하는 방법 

Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int even = 0;
int odd = 0;

even += (number / 10000) % 2 == 0 ? 1 : 0;
even += (number / 1000 % 10) % 2 == 0 ? 1 : 0;
even += (number / 100 % 10) % 2 == 0 ? 1 : 0;
even += (number / 10 % 10) % 2 == 0 ? 1 : 0;
even += (number) % 2 == 0 ? 1 : 0;

odd = 5 - even;

System.out.printf("짝수 : %d개 \n", even);
System.out.printf("홀수 : %d개", odd);

 

 


 

 

자바의 제어 문법 

 

조건문 

if (num>10) {
	print...
    }

 

 

문자열을 입력 받아 같은지 다른지를 비교해보자 

// TODO : 문자열을 입력 받아 서로 비교 -> 같다/다르다 출력

Scanner scanner = new Scanner(System.in);

String word1 = scanner.nextLine();
String word2 = scanner.nextLine();

if (word1.equals(word2)){
    System.out.println("these words are equal");

 

equals 메서드는 완전히 동일해야 같다고 판단한다. 

 

 


if-else 문을 이용해 간단한 계산 로직을 작성해보자. 

 

private static String calculation(int score){
    if (score >=90){
        return "A";
    } else if (score >= 80){
        return "B";
    } else if (score >=70){
        return "C";
    } else if (score >= 60) {
        return "D";
    } else {
        return "F";
    }
}

switch - case는 언제 사용할까? 

 

케이스가 딱 정해져 있을 때 

 

// 1. 햄버거 2. 피자 3. 콜람 4. 기타

Scanner scanner = new Scanner(System.in);

int choice = scanner.nextInt();

switch (choice){
    case 1:
        System.out.println("햄버거");
        break;
    case 2:
        System.out.println("피자");
        break;
    case 3:
        System.out.println("콜라");
        break;
    case 4:
        System.out.println("기타");
        break;
}

 

 


로그인을 검증하는 프로그램을 switch - case 문을 이용해서 만들어보기 

 

private static void practiceLoginVerification() {
    String id = "kosta";
    String password = "1234";

    Scanner scanner = new Scanner(System.in);

    String idInput = scanner.nextLine().trim();
    String pwInput = scanner.nextLine().trim();

    if (id.equals(idInput) && password.equals(pwInput)) {
        System.out.println("로그인 성공");
        return;
    }
    if (!password.equals(pwInput)) {
        System.out.println("비밀번호가 일치하지 않습니다.");
    }
    if (!id.equals(idInput)) {
        System.out.println("id가 일치하지 않습니다.");
    }
}

 

 

 

반응형