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 を明示的に除外したにもかかわらず、上記の 3 つのシナリオでそれぞれ 21、21、および 23 を返すため、これは奇妙です。

どこで間違いを犯したのですか?

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

わかりました、なぜそれが起こるのですか?あなたのロジックは、「いずれかの値が範囲内にある場合は、より大きな値を返し、より大きな値が範囲内にない場合でも」と言っているからです。

代わりに、値が範囲外の場合は考慮しません。考えられる開始点の 1 つを次に示します。

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

あなたのロジックは基本的に次のように述べています。

  • aまたはbが範囲内にあり、aより大きいか等しい場合は、bを返しaます。
  • aまたはbが範囲内にあり、bより大きいか等しい場合は、aを返しbます。
  • 0 を返します。

したがって、aが範囲内にあるbがそれよりも大きい (かつ範囲外である) 場合bでも、 が返されます。

ロジックを次のように変更します。

  • aが範囲内にあり、aより大きいか等しい場合は、bを返しaます。
  • bが範囲内にあり、bより大きいか等しい場合は、aを返しbます。
  • 0 を返します。
于 2013-05-03T21:19:37.113 に答える
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 に答える