public class CharProblem { public static String howOld(final String herOld) { int old = (int)(Math.random()*10); if(old
public class OppositesAttract { public static boolean isLove(final int flower1, final int flower2) { if((flower1 % 2 == 0 && flower2 % 2 == 0)||(flower1 % 2 != 0 & flower2 % 2 !=0)) return false; return true; } } public class OppositesAttract { public static boolean isLove(final int flower1, final int flower2) { return flower1 % 2 != flower2 % 2; } } 아래와 같이 간단한 코드도 있다는 걸 늘 생각하자...
public class Kata { public static String noSpace(final String x) { return x.replaceAll(" ", ""); } }
public class Kata { public static String shortcut(String input) { return input.replaceAll("[aeiou]",""); } } 메서드를 사용하는 쪽이 훨씬 간결하고 편하다는 걸 알고 있어서 저번부터 검색해가며 쓰려고 하고 있는 편이다
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