본문 바로가기

전체 글

(114)
231214 Java 객체지향 클래스 안에 새로운 메소드 만들어서 호출하기 public class HelloWorld { public static void main(String[] args) { HelloWorld helloWorld = new HelloWorld(); for (int i = 0; i < 10; i++) { helloWorld.hello(); } } public void hello(){ System.out.println("Hello ~"); } } 신규 클래스 선언 및 클래스 내부 설정 class Cat { // 클래스 안에 필요한 3가지 // 속성 : 변수 String name; // 초기화를 하지 않음 int aggressive = 50; // 생성자 : 인스턴스를 생성할 때 사용하는 메소드 public Cat(S..
231213 코딩테스트 풀기 프로그래머스 qr code public String solution(int q, int r, String code) { String answer = ""; for(int i = 0; i < code.length(); i++) { if(i % q == r) { answer += code.charAt(i); } } return answer; } 커피 심부름 public int solution(String[] order) { int answer = 0; for(String s : order) { if (s.contains("americano") || s.contains("anything")) { answer += 4500; } else { answer += 5000; } } return answer; } 특정..
231213 Java String substring 명령어 public static void main(String[] args) { String email = "0123456@naver.com"; System.out.println(email.indexOf("@")); // 해당 글자의 위치를 알려주는 명령어 System.out.println(email.substring(3)); // substring(), 3번 인덱스부터 문자열 끝까지 리턴하는 명령어 System.out.println(email.substring(0,email.indexOf("@"))); // 0번 인덱스부터 "@" 가 위치한 인덱스 이전까지 리턴 System.out.println(email.substring(email.indexOf("@"))); // "@..
231212 코딩도장 코딩도장 Multiples of 3 and 5 public static void main(String[] args) { ArrayList list = new ArrayList(); for (int i = 1; i < 1000; i++) { if (i % 3 == 0){ list.add(i); } else if (i % 5 == 0) { list.add(i); } } int sum = 0; for (int i : list) { sum += i; } System.out.println(sum); } 이렇게 풀었는데 그냥 i 값을 합하면 되는 것을.. 탭을 공백문자로 바꾸기 public static void main(String[] args) { String s = "\t asdasdasd"; s = s.rep..
231212 Java 코딩도장 UP & DOWN 숫자 맞추기 게임 public static void main(String[] args) { int random = (int) (Math.random() * 100 + 1); Scanner sc = new Scanner(System.in); int count = 1; System.out.println("컴퓨터가 1 ~ 100 중 랜덤 숫자 하나를 정합니다."); System.out.println("이 숫자를 맞춰주세요."); while (true) { System.out.print("1 ~ 100 숫자 입력 : "); int temp = sc.nextInt(); if (temp > random) { System.out.println("DOWN"); count++; } else i..
231211 프로그래머스 코딩테스트 날짜 비교하기 체크해야할 부분, for 문으로 작은 숫자로만 비교하게 되면 2024.01.31 / 2023.02.01 처럼 연,월,일 중 하나만 비교하게 되어 채점 시 오답이 나옴 public int solution(int[] date1, int[] date2) { int answer = 0; if (date1[0] date2[0]) { // 월 비교하였으나, 연도를 다시 한번 비교 answer = 0; } } if (date1[2] date2..
231211 Java while 로 점수 입력받기, 범위를 벗어나면 다시 입력받기 public static void main(String[] args) { boolean run = true; int score; Scanner sc = new Scanner(System.in); while (run) { System.out.println("점수를 입력하세요. ( 0 ~ 100 )"); System.out.print("입력 : "); score = sc.nextInt(); if (score 100){ System.out.println("올바른 숫자가 아닙니다. 다시 입력해주세요"); continue; } run = false; } System.out.println("프로그램 종료"); } 가위바위보를 w..
231208 프로그래머스 코딩테스트 특이한 이차원 배열 2 public int solution(int[][] arr) { int answer = 0; for (int i = 0; i < arr.length; i++) { for(int j = 0; j < arr[i].length; j++){ int a = arr[i][j]; int b = arr[j][i]; if (a == b) { answer = 1; } else { answer = 0; return answer; } } } return answer; } 순서 바꾸기 public int[] solution(int[] num_list, int n) { int[] answer = new int[num_list.length]; int temp = n; for(int i = 0; i < num_..