자바 기초 복습

장수빈·2026년 7월 22일

인프런 자바 기초강의 예전에 보고 벨로그 써놓았던거 다시 보고 복습하려고 했는데 정리가 잘 안되어있어서 programmers 기초 코드 테스트로 대체하여 진행했습니다.......😂

문자열 붙여서 출력하기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        System.out.print("str1: ");
        String str1 = scanner.next();
        
        System.out.print("str2:");
        String str2 = scanner.next();
        
        System.out.print(str1 + str2);
    }
    
}

Scanner 객체 생성 sc로 되어있는데 Scanner로 써서 오류
System.out.print("str1:") 같은 안내문구 없이 그냥 값만 입력받아야 함

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        String str1 = sc.next();
        
        String str2 = sc.next();
        
        System.out.print(str1 + str2);
    }
    
}

str3 = str1+str2 이런식으로 해서
Ststem.out.print(str3)도 가능

문자열 출력하기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        
        System.out.print(a);
    }
}

(이게 먼저였으..)

a와 b 출력하기

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();

        System.out.print("a = ");
        System.out.println(a);
        System.out.print("b = ");
        System.out.print(b);
    }
}

첨에 생각한 코드

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();

        System.out.println("a = " + a + "\n" + "b = " + b);
    }
}

"\n"으로도 줄바꿈이 되는줄 몰랐다ㅏ.
백슬래시: \ 누르면 백슬래시로 입력됨

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();

        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}

이게 내가 처음 쓴 것보다 더 편할 것 같다.

문자열 반복해서 출력하기⭐

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        int number = 0;
        
        while (number<=n){
            System.out.print("str");
            number = number + 1;
        }
            
        }
    }
}

"str"으로 sout안에 써서 문자열 str 출력됌
중괄호 하나 더 씀
number = 0으로 설정해놓아서 number <= n이면 6번 출력됌

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        int number = 0;
        
        while (number<n){
            System.out.print(str);
            number ++;
        }
    }
}

number ++ ; 사용하면 1씩 증가해서 편함

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();
        
        System.out.print(str.repeat(n));
    }
}

.repeat(n) 사용하면 n만큼 반복

덧셈식 출력하기

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 result = (a + b);

        System.out.println(a + " + " + b + " = " + result);
    }
}

result = (a+b);에 괄호 없어도 됌

홀짝 구분하기

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.println(n + " is even");
        } else{
                System.out.println(n + " is odd");
        }
    }
}

대소문자 변환하기⭐

for 반복문

for(초기화식; 조건식; 증감문){
	반복될 동작}

while 반복문

while(조건식){
	반복문과 증감문}

Character.isUpperCase / CharactertoUpperCase

영문의 대소문자를 구분해준다.
Character.isUpperCase는 대문자인지 판별하고
Character.toUppercase는 대문자로 변환시킬 때 사용한다.
소문자 판별할때 Character.isLowerCase / 소문자로 바꿀때 Character.toLowerCaser

문자열의 i번째 글자를 가져올때

char ch = a.charAt(i)

ch라는 변수에 a라는 문자열의 i번째 글자를 저장하겠다.

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        
        for(int i = 0; i < a.length(); i++){
            char ch = a.charAt(i);
            
            if (Character.isUpperCase(ch)){
                ch = Character.toLowerCase(ch);
                System.out.print(ch);
            } else{
                ch = Character.toUpperCase(ch);
                System.out.print(ch);
            } 
        }
    }
}

특수문자 쓰기⭐

백슬래시1개 -> 백슬래시2개
" -> 백슬래시"

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        
        System.out.print("!@#$%^&*(\\'\"<>?:;");
    }
}

문자열 90도 돌리기

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        
        for (int i = 0; i<a.length(); i++){
            char chr = a.charAt(i);
            System.out.println(chr);
        }
    }
}

n의 배수


class Solution {
    public int solution(int num, int n) {
        int answer = 0;
        
        if (num% n == 0){
            return answer = 1;
        } else {
            return answer = 0;
        }
    }
}

이미 첨에 int answer = 0; 이라고 되어있어서 마지막에 return answer = 0; 한번 더 붙일 필요 없으

0개의 댓글