공부/프로그래머스 연습

프로그래머스 - 자바 기초트레이닝 (Lv.0) - 출력, 연산 2

록's 2023. 8. 1. 14:14
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
반응형