✅ 문제 

// 1에서 100사이의 홀수 개수를 출력하세요.

 

✅ 문제해설

더보기
  • 홀수를 2로 나누면 항상 나머지가 1이 생긴다. 
  • % (나머지) 연산자를 사용해서 1이 나오면 출력하게 구현한다.

 

 

public static void main(String[] args) {
int oddnum = 0; //홀수 개수 초기화.

//oddnum을 2로 나눈 나머지가 1이면 oddnum에 +1씩 증가시켜줘서 카운딩한다.
for (int i = 0; i < 100; i++) {
	if ((i % 2) == 1) {
		oddnum++;
	}
}
System.out.printf("홀수는 %d개 입니다.\n", oddnum);
}

 

✅ 결과

 

✅ 문제 

// 시험 점수를 입력받아서 성적을 출력하세요.
// A : 90~100, B : 80~89, C : 70~79, D : 60~69, F : ~59

 

✅ 문제 해설

더보기

각 등급에 맞게 하나씩 조건문으로 조건에 맞으면 결과 출력

 

public static void main(String[] args) {

		// 시험 점수를 입력받아서 성적을 출력하세요.
		// A : 90~100, B : 80~89, C : 70~79, D : 60~69, F : ~59

		Scanner sc = new Scanner(System.in); //시험 점수 입력받기

		System.out.print("성적 입력 : ");
		int grade = sc.nextInt(); //Grade에 성적 값 저

		// 90이상 100이하이면 "A" 출력
		if (90 <= grade && grade <= 100) {
			System.out.println("당신의 성적은 A");
		}
		// 80이상 90미이면 "B" 출력
		if (80 <= grade && grade < 90) {
			System.out.println("당신의 성적은 B");
		}
		// 70이상 80미만이면 "C" 출력
		if (70 <= grade && grade < 80) {
			System.out.println("당신의 성적은 C");
		}
		// 60이상 70미만이면 "D" 출력
		if (60 <= grade && grade < 70) {
			System.out.println("당신의 성적은 D");
		}
		// 60미만이면 "F" 출력
		if (grade < 60) {
			System.out.println("당신의 성적은 F! 공부하세요");
		}
}

 

✅ 결과 

 

✅ 문제

✅ 풀이

 

이클립스에서 문자열 메소드를 찾다가 편한것을 발견했따!.

 

(문자열).repeat(반복횟수)

 

repeat 메소드를 사용하면 편리하게 문자열을 반복할 수 있다.

🟩 String java.lang.String.repeat(int count)

Returns a string whose value is the concatenation of this string repeated count times.

If this string is empty or count is zero then the empty string is returned.

Parameters:
count number of times to repeat
Returns:
A string composed of this string repeated count times or the empty string if this string is empty or count is zero
Throws:
IllegalArgumentException - if the count is negative.
Since:
11

 

💢 아래 해결 코드!

class Solution {
    public String solution(String my_string, int k) {
        String answer = "";
        return my_string.repeat(k);
    }
}

뭔가 메소드의 도움을 받아 해결해서 내가 푼거 같지 않아서...내가 알고 있는 지식으로 재도전! 👊🏼

 

class Solution {
    public String solution(String my_string, int k) {
        String answer = "";
        
        for(int i=0;i<k;i++)
            answer+=my_string;
        
        return answer;
    }
}

 

위 방식으로 해결했다.👊🏼 👊🏼 👊🏼

✅ 문제

 

✅ 코드

class Solution {
    public String solution(String[] arr) {
        String answer = "";
        
        for (int i =0; i<arr.length;i++) {
        	answer+=arr[i];
        }
        
        return answer;
    }
}

✅ 문제

 

 

✅ 코드

 

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

 

✅ 문제

✅ 코드

 

class Solution {
    public String solution(String str1, String str2) {
       String answer = "";

		for(int i=0;i<str1.length();i++) {
			answer=answer+str1.charAt(i)+str2.charAt(i);
		}
		
		return answer;
    }
}

+ Recent posts