2013-05-03 59 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; 
相關問題