일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- 유비쿼터스
- for
- Scanner
- C언어
- IFELSE
- MySQL
- error
- Scanner class
- 기본
- 백준
- 배열
- java
- 알고리즘
- 함수
- 반복문
- 스캐너
- 1차원배열
- java프로그래밍
- 하드웨어
- IF
- 파이썬프로그래밍기초
- IF문
- 스캐너클래스
- FOR문
- 자료구조
- Spring
- 백준알고리즘
- 변수
- 자바
- 데이터
Archives
- Today
- Total
정리하고기록하자
백준 (JAVA) - N 찍기 (2741) 본문
반응형
백준 - 알고리즘 for문 N 찍기 (2741) 문제
문제 :
자연수 N이 주어졌을 때, 1부터 N까지 한 줄에 하나씩 출력하는 프로그램을 작성하시오.
문제에서 자연수 N 이 주어졌다고 했으니
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
}
}
입력할 a 변수를 선언해줘야 겠지 ?
예제 입력 을 5로 했고
예제 출력이 1 2 3 4 5 가 출력되니까
for(int i = 1; i <= a; i++){
System.out.println(i);
}
합치면
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i = 1; i <= a; i++){
System.out.println(i);
}
}
}
끝!
처음 컴파일 에러
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i = 0; i <= a; i++){
System.out.println(ㅑ);
}
}
}
System.out.println(ㅑ);
... 세상에
두번째 틀렸습니다.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i = 0; i <= a; i++){
System.out.println(i);
}
}
}
출력은 1부터 인데 나는 0부터 for문을 돌렸으니 틀렸고
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
for(int i = 1; i <= a; i++){
System.out.println(i);
}
}
}
성공~
반응형
'백준 - 알고리즘' 카테고리의 다른 글
백준 (JAVA) - 별찍기 -1 (2348) (0) | 2021.11.07 |
---|---|
백준 (JAVA) - 기찍 N (2742) (3) | 2021.11.04 |
백준 (JAVA) - 빠른 A+B (15552) (0) | 2021.11.03 |
백준 (JAVA) - 합 (8393) (0) | 2021.10.19 |
백준 (JAVA) - A+B-3 (10950) (0) | 2021.10.19 |