728x90
반응형
기초 트레이닝 (Lv.0)
day 1
Java 자바
프로그래머스 출력,연산 연습
1. 덧셈식 출력하기
소스 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = a + b;
System.out.println(a + " + " + b + " = " + c);
}
}
2. 문자열 붙여서 출력하기
1) 소스 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
System.out.print(a+b);
}
}
2) 소스 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
String b = sc.next();
String result1 = a.trim();
String result2 = b.trim();
System.out.print(result1);
System.out.print(result2);
}
}
3. 문자열 돌리기
소스 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.next();
int l = a.length();
for(int i=0; i<l; i++) {
System.out.println(a.charAt(i));
}
}
}
4. 홀짝 구분하기
소스 코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n % 2 ==0) {
System.out.print(n + " is even ");
System.out.println();
} else {
System.out.print(n + " is odd ");
System.out.println();
}
}
}
5. 문자열 겹쳐쓰기
소스코드
class Solution {
public String solution(String my_string, String overwrite_string, int s) {
String answer = "";
int a = overwrite_string.length();
int b = my_string.length();
answer = my_string.substring(0,s) + overwrite_string + my_string.substring(s+a, b);
return answer;
}
}
728x90
반응형
'공부 > 프로그래머스 연습' 카테고리의 다른 글
프로그래머스 - 문자 리스트를 문자열로 변환하기 Lv.0 (0) | 2023.08.02 |
---|---|
프로그래머스 - 코딩테스트 연습 - 문자열 섞기 Lv.0 (0) | 2023.08.02 |
프로그래머스 - 자바 기초 트레이닝 (Lv.0) - 출력 1 (0) | 2023.08.01 |