いずれかの値が範囲内にある場合、コードは大きい方の値を返します。
これらの質問の性質を考えると、メソッドを開発するためにテスト主導のアプローチを試す必要があります。これにより、コードが意図したとおりに動作することも保証されます。以下のようなテストは、CodingBat でコードを送信するときにコードをテストしているものと思われます。
public class SandBox {
public int max1020(int a, int b) {
if (10 <= a && a <= 20) { // if a is in range
if (a >= b || b > 20) { // if a is greater than B or B is out of range
return a;
}
}
//
if (10 <= b && b <= 20) { // if b is in range
return b;
}
return 0;
}
}
テスト
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
public class SandBoxTest {
SandBox sand;
@Before
public void given(){
sand = new SandBox();
}
@Test
public void testOne(){
int i = sand.max1020(1, 2);
assertThat(i, is(0));
}
@Test
public void testTwo(){
int i = sand.max1020(2, 1);
assertThat(i, is(0));
}
@Test
public void testThree(){
int i = sand.max1020(5, 10);
assertThat(i, is(10));
}
@Test
public void testFour(){
int i = sand.max1020(10, 5);
assertThat(i, is(10));
}
@Test
public void testFive(){
int i = sand.max1020(11, 15);
assertThat(i, is(15));
}
@Test
public void testSix(){
int i = sand.max1020(15, 11);
assertThat(i, is(15));
}
@Test
public void testSeven(){
int i = sand.max1020(20, 23);
assertThat(i, is(20));
}
@Test
public void testEight(){
int i = sand.max1020(23, 20);
assertThat(i, is(20));
}
@Test
public void testNine(){
int i = sand.max1020(33, 25);
assertThat(i, is(0));
}
@Test
public void testTen(){
int i = sand.max1020(25, 33);
assertThat(i, is(0));
}
}