1

I have kept at these CodingBat questions and I ran into another problem.

The assignment was this:

Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.

Some Examples:

  • max1020(11, 19) → 19
  • max1020(19, 11) → 19
  • max1020(11, 9) → 11

My solution is:

public int max1020(int a, int b) {
    if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && a >= b) {
        return a;
    }

    if (((a >= 10 && a <= 20) || (b >= 10 && b <= 20)) && b >= a) {
        return b;
    } else {
        return 0;
    }
}

The code returned the correct output more than half of the time, but it did not work as expected in certain scenarios. The scenarios are when the input was (10,21),(21,10),(23,10).

This is weird because I explicitly excluded #>20 yet it returns 21, 21, and 23 respectively for the above three scenarios.

Where did I make a mistake?

4

4 回答 4

4

让我们来看看它们。

(10,21):

if(((10 >= 10 && 10 <= 20) || (21>=10 && 21<=20)) && 10>=21)
  (((true and true) || (true and false)) && false)
  ((true) && false)
  false

好吧,不是那个。

if(((10 >= 10 && 10 <= 20) || (21>=10 && 21<=20)) && 21>=10)
  (((true and true) || (true and false)) && false)
  ((true) and true)
  true

-> return 21

好的,为什么会这样?因为您的逻辑说“如果任一值在范围内,则返回较大的值,即使较大的值不在范围内”。

相反,如果一个值超出范围,甚至不要考虑它。这是一个可能的开始:

if(a < 10 && a > 20) {
    // do something with only b. This is where you would return zero, too.
} else if(b < 10 && b > 20) {
    // do something with only a
} else {
    // do something with both
}
于 2013-05-03T21:20:46.720 回答
3

你的逻辑基本上说:

  • 如果aorb在范围内,并且a大于或等于b,则返回a
  • 如果aorb在范围内,并且b大于或等于a,则返回b
  • 返回 0。

因此,如果a在范围内但b更大(并且超出范围),则b仍然返回。

将您的逻辑更改为:

  • 如果a在范围内并且a大于或等于b,则返回a
  • 如果b在范围内并且b大于或等于a,则返回b
  • 返回 0。
于 2013-05-03T21:19:37.113 回答
0
Boolean aInRange = (a >= 10 && a <= 20)

Boolean bInRange = (b >= 10 && b <= 20)

int max = 0;

if (aInRange && bInRange) {

    if(a >= b)
        max = a;
    else {
        max = b;
    }
} else if (!bInRange && aInRange) {
    max = a;
}
else if (bInRange && !aInRange) {
    max = b;
}
return max;
于 2013-05-06T13:00:07.580 回答
0

如果任一值在范围内,您的代码将返回较大的值。

鉴于这些问题的性质,您应该尝试一种测试驱动的方法来开发您的方法,这也可以确保您的代码按照您的预期运行。当您在 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));
    }
}
于 2013-05-03T21:24:43.393 回答