[TIL] 20240303

Xtraa·2024년 3월 3일
0

TIL

목록 보기
39/99

공부한 내용

1. Java 복습

  • 클래스와 데이터

2. 코딩테스트

  • 프로그래머스
    • 코딩테스트 입문
    • 코딩 기초 트레이닝

문제

문자열 반복해서 출력하기

// 내가 작성한 코드
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int n = sc.nextInt();

        for (int i = 0; i < n; i++) {
            System.out.print(str);
        }
    }
}

// 새롭게 알게된 메서드
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.println(str.repeat(n));
    }
}

for문을 사용해서 반복 출력을 했는데 repeat()이라는 메서드를 사용해서 문자열 반복 출력이 가능하다


대소문자 바꿔서 출력하기

import java.util.*;

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

[Java] 대/소문자 변경


특수문자 출력하기

import java.util.Scanner;

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

역슬래시를 그대로 출력하기 위해 \앞에 역슬래시를 한번 더 입력해주고,

큰 따옴표를 그대로 출력하기 위해 " 앞에 역슬래시를 입력한다.


하루를 돌아보며, TMI

IDE의 자동 완성 기능을 사용하다가 직접 치려고 하니까 당황스럽다.

sout만 생각나고… Sytem.out.print가 헷갈리다니ㅋㅋㅋㅋ


참고자료

profile
https://xtraa.notion.site/Xtraa-ed48ac432d354d01b5bf5b0da5ec94a9?pvs=4

0개의 댓글