본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] warmup -1 단계 : sumDouble 문제

728x90
728x90

https://codingbat.com/prob/p154485

 

CodingBat Java Warmup-1 sumDouble

Given two int values, return their sum. Unless the two values are the same, then return double their sum.sumDouble(1, 2) → 3sumDouble(3, 2) → 5sumDouble(2, 2) → 8Go...Save, Compile, Run (ctrl-enter)Show Solution

codingbat.com



= 문제 번역 =

두 개의 int 값이 주어지면 그 합을 반환합니다. 두 값이 같지 않으면 합을 두 배로 반환합니다.

 

= 해설 =

 

< 1 >
public int sumDouble(int a, int b) {
  if(a==b) return (a+b)*2;
  else return a+b;
}

< 2 >
public int sumDouble(int a, int b) {
	if (a != b) {
		return a + b;
	} else {
		return (a + b) * 2;
	}
}

728x90