전체 글 52

Lv1 - 핸드폰 번호 가리기

class Solution { public String solution(String phone_number) { String answer = ""; int length = phone_number.length(); return answer = "*".repeat(length-4) + phone_number.substring(length-4); }} 간단하게 입력된 전화번호에서 마지막 4번째를 제외하고 " * "로 바꿔준 뒤 출력하면 되는 간단한 문제이다.처음에는 문제를 보고나서 문자열 길이를 구하고 11자리일때와 10자리일때 2가지를 통해서 코드를 작성해보려고 했었으나,repeat, substring 메서드를 통해 간단히 해결할 수 있었다.repeat - "..

Lv1 - 두 개 뽑아서 더하기

import java.util.*;class Solution { public int[] solution(int[] numbers) { Set set = new HashSet(); // 중복을 제거하는 HashSet 사용 // 두 개의 숫자를 선택해서 더하는 과정 for (int i = 0; i 배열 변환 후 정렬 int[] answer = set.stream().mapToInt(Integer::intValue).toArray(); Arrays.sort(answer); // 오름차순 정렬 return answer; }} 전에 사용했던 HashSet, Set을 사용하여 중복을 제거하려고 했다. 하지만 배열 변환을 하는 법..

Lv1 - 음양더하기

class Solution { public int solution(int[] absolutes, boolean[] signs) { int answer = 123456789; int result = 0; for(int i=0;i 이 문제는 간단한 원리만 알고있다면 편하게 풀 수 있는 문제였다.다른 사람의 문제풀이를 보니class Solution { public int solution(int[] absolutes, boolean[] signs) { int answer = 0; for (int i=0; i이런식으로 푼 사람도 있었다. 이 코드가 정말 간결하게 작성을 잘한 것 같다.

Lv1 - 숫자 문자열과 영단어

class Solution { public int solution(String s) { String[] num = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}; String[] word = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; for(int i=0; i num이라는 String배열을 사용하여 0~9까지 들어가있는 배열을 생성한다.word라는 String배열을 사용하여 0~9까지 단어값이 들어가있는 배열을 생성한다. 배열의 크기는 어차피 10이므로 10번간 String s의  word[i]값을 num[i]값으로..

Lv1 - 부족한 금액 계산하기

간단하게 설명을 하게 된다면 놀이기구의 이용료는 price -> n번 이용할때마다 price = price *n으로 높아지게 되는데 가지고 있는 돈 내에서 어느정도까지 이용이 가능한가를 구하는 코드이다. 처음에는 for문과 if문을 사용하여 코딩을 막해보다가 간단히 몇줄로 요약되었다. class Solution { public long solution(int price, int money, int count) { long totalCost = 0; //총 count번까지 이용하려면 필요한 돈 for(int i = 1; i 0 ? shortCost : 0; // 부족한돈이 없으면 0 반환 }} 이제부터는 코드를 작성할 때 충분히 고민해보고 풀게되면 불필요하게 코드 길이를..

Jump to Java - 8장 프로그래밍 예제

1. 구구단 프로그램 만들기package problem;import java.util.Scanner;public class problem1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //Scanner로 수 입력받기 System.out.print("원하는 구구단 수를 입력하세요 : "); int x = scanner.nextInt(); // x에 입력받은 값 저장 for (int i = 1; i  2. 1~1000까지 정수중에 3, 5의 배수의 합을 구하기package problem;public class problem2 { public ..

JAVA 기초공부 2025.02.18

JAVA - 예외처리, try - catch문

예외발생 경우1. 없는 파일을 읽으려고 할 때import java.io.*;public class Sample { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("나없는파일")); br.readLine(); br.close(); }} 이 코드를 실행하면 존재하지 않는 파일을 열려고 시도했기 때문에 원하는 파일을 찾을 수 없다는 FileNotFoundException라는 예외가 발생한다.2. 4 / 0 과 같이 연산할 수 없는걸 연산할 경우public class Sample { public stat..

JAVA 기초공부 2025.02.17