본문 바로가기

JAVA

자바 1부터 N까지 출력하기 (백준 2741번 N 찍기)

1) bufferedreader를 이용해 숫자 N을 입력받는다

2) string으로 읽힌 숫자를 parseInt 메소드를 이용해 int로 형변환한다.

3) while loop를 사용해서 1부터 N에 도달할 때까지 숫자를 하나씩 늘려가며 출력한다. 

import java.io.*;

class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        
        int num = Integer.parseInt(br.readLine());
        int count = 1;
        
        while(count<=num){
            bw.write(Integer.toString(count));
            bw.newLine();
            
            count++;
        }
        
        bw.flush();
    }
}

 

1부터 100까지 출력하고 싶은 경우 num을 입력받는 대신 100으로 지정해주면 된다.