반응형

# 반복문의 break, continue

  • 반복문에서 break 가 나올 경우 반복문을 중단 시킨다.
class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                //i가 5일 때 반복문을 중단시킨다.
                break;
            }
            System.out.println("Coding Everybody " + i);
        }
    }
}
  • 반복문에서 continue 가 나올 경우 이후의 로직은 실행하지 않는다. 하지만 반복문 자체를 중단하는 것이 아니라 반복문으로 돌아간다. 

 

class Main {
    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            if (i == 5) {
                //i가 5일 때 실행을 중단하고 반복문은 계속 실행된다.
				// continue 시 밑에 실행하지 않고 반복문으로 돌아간다. ( i 가 5일 때 출력은 스킵된다. )
                continue;
            }
            System.out.println("Coding Everybody " + i);
        }
    }
}
반응형
반응형

# [구름] 연산의 우선순위

  • 프로그래밍을 하게 되면 다양한 연산자들을 복합적으로 사용.
  • 이때 연산의 선후 관계가 분명하지 않으면 혼란이 발생한다.
1 [] () . 
2 ++ -- + - ~ ! (type) new
3 * %
4 +(더하기) -(빼기) +(문자 결합 연산자)
5 << >> >>>
6 < <= > >= instanceof
7 == !=
8 &
9 ^
10 |
11 &&
12 ||
13 ? :
14 = *= /= += -= %= <<= >>= >>>= &= ^= !=
반응형
반응형

# 주석 (Comment)

  • 로직에 대한 설명, 코드를 비활성화 할 때 사용한다.
  • 주석은 프로그래밍적으로 해석되지 않는다.
1. 한줄 주석
public static void main(String[] args) {

	// 주석 입니다.
    int a;
}


2. 여러줄 주석
public static void main(String[] args) {

	/* 여러줄 주석 입니다.
    
    int a;
    
    */
}


3. JavaDoc 주석
- 주석이면서 동시에 자바의 api 문서를 만드는 규약이 있다.

/**

* Prints an integer and then terminate the line. This method behaves as

* though it invokes <code>[@link #print(int)}</code> and then

* <code>{@link #println()}</code>

*

* @param x The <code>int</code> to be printed.

*/

public void println(int x) {
	synchronized (this) {
    	print(x);
        
        newLine();
    }
}

# 세미콜론 (statement)

  • 문장의 끝을 의미한다.
  • 자바에서는 문장의 끝에 세미콜론을 사용하지 않으면 컴파일 에러가 발생한다.
int a = 10;

int k = 5; int b = 12;

System.out.println("Hello World");
반응형
반응형

# 백준 2869 달팽이는 올라가고 싶다 자바

  • 달팽이가 목표지점까지 올라가는데 걸리는 날짜 구하기.
  • 낮에 A만큼 올라갈 수 있고, 밤에 자는동안 B만큼 떨어지며, 정상에 올라가면 떨어지지 않는다.
  • 첫째 줄에 A, B, 목표지점 V 제공 될 때 몇일걸리는 지 출력.
  • Scanner 사용 시 시간초과 되므로, Buffered 사용.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.StringTokenizer;
 
public class Main {
 
	public static void main(String[] args) throws IOException {
 
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine(), " ");
        
		int A = Integer.parseInt(st.nextToken());
		int B = Integer.parseInt(st.nextToken());
		int V = Integer.parseInt(st.nextToken());
 
		int day = (V - B) / (A - B);

		if ((V - B) % (A - B) != 0) {
			day++;
		}
		System.out.println(day);
	}
}
반응형

+ Recent posts