2026-10-01

jun0·어제

프로그래머스 코딩 기초 트레이닝

문제 설명 : 정수 a 와 b 가 주어집니다. 각 수를 입력받아 입출력 예와 같은 형식으로 출력하는 코드를 작성해 보세요.

입력 : 4 5

출력 :
a = 4
b = 5

1 import java.util.Scanner;
2
3 public class Solution {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 int a = sc.nextInt();
7 int b = sc.nextInt();
8
9 System.out.println(a + b);
10 }
11}

풀이 :

1 import java.util.Scanner;
2
3 public class Solution {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 int a = sc.nextInt();
7 int b = sc.nextInt();
8
9 System.out.println("a = " + a);
10 System.out.println("b = " + b);
11 }
12}

처음으로 통과한 문제다
근데 다른사람의 풀이를 보니 나는 스캐너를 안닫았다
sc.closer();
방금 또 찾아보니 상황에 따라 사용하는게 좋으니 알아두자

문2
문제설명 : 문자열 str과 정수 n이 주어집니다.
str이 n번 반복된 문자열을 만들어 출력하는 코드를 작성해 보세요.

입력 :
string 5
stringstringstringstringstring

풀이 :
1 import java.util.Scanner;
2
3 public class Solution {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6 String str = sc.nextLine();
7 int n = sc.nextInt();
8
9 System.out.println(str.repeat(n));
10 }
11 }

이거는 구글링으로 검색해 봤는데 바로 나왔다
이런 함수도 있는게 신기했다 문자열을 곱하는 함수도 있네...
간편하고 없는게 없는거 같다

0개의 댓글