public class EvenOrOdd { public static String even_or_odd(int number) { if(number % 2 == 0){ return "Even"; }else{ return "Odd"; } } } public class EvenOrOdd { public static String even_or_odd(int number) { return (number % 2) == 0 ? "Even" : "Odd"; } } 이거 써봐야지, 마음 먹고 풀지 않는 이상 항상 if문을 쓴 다음에야 삼항연산자가 생각난다... 앞으로는 까먹지 않게 삼항연산자를 써야지
public class GrassHopper { public static int findAverage(int[] nums) { int sum = 0; for(int num : nums) { sum += num; } return sum / nums.length; } }
class Kata { public static String multiTable(int num) { String str = ""; for(int i = 1; i < 10; i++){ str += (i + " * " + num + " = " + (i*num) + "\n"); } str += ("10 * " + num + " = " + (10*num)); return str; } } 처음에는 10까지 true로 넣었는데 안 돼서 왤까 생각해보니 뒤에 줄바꿈 때문인가 해서 10은 밖으로 빼줬더니 됐다
public class AbbreviateTwoWords { public static String abbrevName(String name) { String[] nm = name.split(" "); return (nm[0].charAt(0) + "." + nm[1].charAt(0)).toUpperCase(); } } 대문자 변환!! 이걸 안 해서 왜 안 되냐고 붙들고 있었다
public class CuboidVolumes { public static int findDifference(final int[] firstCuboid, final int[] secondCuboid) { //your code here !! return Math.abs(firstCuboid[0]*firstCuboid[1]*firstCuboid[2] - secondCuboid[0]*secondCuboid[1]*secondCuboid[2]); } }
public class Paper { public static int paperWork(int n, int m) { if(n
public class Banjo { public static String areYouPlayingBanjo(String name) { if(name.startsWith("r") || name.startsWith("R")) { return name + " plays banjo"; } else { return name + " does not play banjo"; } } }
package com.codewars.hwtdstrngls; public class RoundToTheNextMultipleOf5 { public static int roundToNext5(int n) { while( n % 5 != 0) n++; return n; } } 몇 줄 안 되는데 여전히 시간이 오래 걸린다 백준 같은 사이트 문제는 더 오래 걸려서 시간이 있는 날 없는 날 백준 / 코드워즈를 왔다 갔다 하는 중
public class Solution { public static String repeatStr(final int repeat, final String string) { String result = ""; if (repeat > 0) { for (int i = 0; i < repeat; i++) { result += string; } } return result; } }
public class NoBoring { public static int noBoringZeros(int n) { if(n == 0) { return n; } while(n % 10 == 0) { n /= 10; } return n; } }