2013-05-03 30 views
1

我一直在这些CodingBat问题,我遇到了另一个问题。在一个范围内寻找更大的数字

分配是这样的:

鉴于2倍阳性INT的值,则返回该值越大,即在该范围内10..20以下,或返回0,如果既不是在该范围内。

一些例子:

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

我的解决方案是:

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; 
    } 
} 

该代码在超过一半的时间内返回了正确的输出,但在某些情况下未按预期工作。情景是当输入是(10,21),(21,10),(23,10)时。

这很奇怪,因为我明确排除#> 20,但它分别返回上述三种情况下的21,21和23。

我在哪里犯了一个错误?

+0

尝试写你的比较作为'(10 = 2013-05-03 21:19:36

+0

我不知道在写这里的时候是否错过了它,但是在第二个if之前应该有'else'。还有其他问题,但你应该先解决这个问题。 – SOfanatic 2013-05-03 21:22:39

回答

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 
} 
+0

很好的解释 – user2281527 2013-05-03 21:25:43

+0

这是一个很好的分析方法我觉得我现在可以得到它。要再试一次解决它。 – LearnIT 2013-05-03 21:27:34

+0

我结束了使用完全不同的方法,但感谢彻底的回应,帮助我看看我的逻辑链有点不同。 – LearnIT 2013-05-04 00:41:16

3

你的逻辑基本上是说:

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

所以,如果a在范围内,但b较大(超过范围),然后b仍会返回。

你的逻辑更改为:

  • 如果a在范围和a大于或等于b,然后返回a
  • 如果b在范围内且b大于或等于a,则返回b
  • 返回0.
0

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

鉴于这些问题的性质,您应该尝试test driven approach来开发您的方法,这也可以确保您的代码按照您的预期行事。类似于以下内容的测试可能是您在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)); 
    } 
} 
+0

这是错的btw – LearnIT 2013-05-04 00:40:17

+0

@LearnIT谢谢你的抬头 – 2013-05-06 20:58:37

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;