Programming problem at
http://codingbat.com/prob/p154485
Given two int values, return their sum. Unless the two values are the same, then return double their sum.
sumDouble(1, 2) → 3
sumDouble(3, 2) → 5
sumDouble(2, 2) → 8
My Solution
public int sumDouble(int a, int b) {
if(a==b)
return 2*(a+b);
else
return(a+b);
}
The official solution was
public int sumDouble(int a, int b) {
// Store the sum in a local variable
int sum = a + b;
// Double it if a and b are the same
if (a == b) {
sum = sum * 2;
}
return sum;
}
Comparison and Lessons
Missed semi colons in first run , always check sytax errors
No comments:
Post a Comment
Please share your views and comments below.
Thank You.