switch语句不起作用(怪异地“跳转”)

时间:2013-12-04 17:44:23

标签: java android switch-statement

我使用了一些非常简单的开关......但它不起作用。我只是在开关中看不到任何错误......

我在我的代码中添加了3条评论,以便在我输入type == BODYSIZE时达到或未达到哪些分数...

我不知道它是如何发生的,“1)”到达而“2)”不是......但是调试正好向我显示了这个......它只是跳到“3)”之后“1)” ...

我尝试从手机中删除应用程序,删除bin / gen文件夹并重建项目,但问题确实存在于代码中......我只是看不到它......

public static void getMinMaxValuesForNumberPicker(LengthType type, IntegerHolder min1, IntegerHolder max1, IntegerHolder min2, IntegerHolder max2)
{
    switch (type)
    {
        case BODYSIZE: // cm bzw. ft + in
        {
            // 0 - 3m (3m ~ 9.84ft)
            if (getCurrentLengthUnit() == LengthUnit.METER)
            {
                min1.set(0);
                max1.set(300);
            }
            else
            {
                min1.set(0);
                max1.set(10);
                min2.set(0);
                max2.set(11);              // <= 1) IS REACHED
            }
            return;                     // <= 2) IS NOT REACHED
        }
        case CIRCUMFERENCE: // cm bzw. in
        { // 0 - 500cm (500cm ~ 196.85in)
            if (getCurrentLengthUnit() == LengthUnit.METER)
            {
                min1.set(0);
                max1.set(500);
            }
            else
            {
                min1.set(0);
                max1.set(200);
            }
            return;
        }
        case WRINKLE: // cm bzw. in
        { // 0 - 50cm (50cm ~ 19.69in)
            if (getCurrentLengthUnit() == LengthUnit.METER)
            {
                min1.set(0);
                max1.set(50);
            }
            else
            {
                min1.set(0);
                max1.set(20);
            }
            return;
        }
        case DISTANCE: // km + m bzw. mi + yd
        { // 0 - 1000km (1000km ~ 621.37mi)
            if (getCurrentLengthUnit() == LengthUnit.METER)
            {
                min1.set(0);
                max1.set(1000);
                min2.set(0);
                max2.set(999);
            }
            else
            {
                min1.set(0);
                max1.set(500);
                min2.set(0);
                max2.set(1759);
            }
            return;                       // <= 3) IS REACHED
        }
        default:
            throw new RuntimeException("getMinMaxValuesForNumberPicker für " + type.name() + " nicht implementiert!");
    }
}

3 个答案:

答案 0 :(得分:2)

switch (key)
     {
        case value :{
           //do something...
        }
           break;

        default :
           break;
     }

我的假设是,回报并没有结束转换 - 但我不确定这一点。

答案 1 :(得分:1)

事实上,您的代码没有任何问题,它工作正常。我猜你正在使用eclipse,然后当你用Android进行调试时,它总是到达方法的最后一个返回行,它只是到达那里但不执行该行(这应该是eclipse的错误)。

我在你的代码中添加了一些日志:

public static void getMinMaxValuesForNumberPicker() {
        Log.i("TestActivity", "Your method *******************");

        int type = 0;
        int min1 = 0, min2 = 0, max1 = 0, max2 = 0;

        switch (type) {
        case 0: // cm bzw. ft + in
        {
            // 0 - 3m (3m ~ 9.84ft)
            if (min1 > min2) {
                min1 = 0;
                max1 = 300;
            } else {
                min1 = 0;
                max1 = 10;
                min2 = 0;
                max2 = 11; // <= 1) IS REACHED
                Log.i("TestActivity", " 1) IS REACHED");
            }
            Log.i("TestActivity", " 2) IS REACHED");
            return; // <= 2) IS NOT REACHED
        }
        case 1: // cm bzw. in
        { // 0 - 500cm (500cm ~ 196.85in)
            if (min1 == 1) {
                min1 = 0;
                max1 = 500;
            } else {
                min1 = 0;
                max1 = 200;
            }
            return;
        }
        case 2: // cm bzw. in
        { // 0 - 50cm (50cm ~ 19.69in)
            if (min1 < min2) {
                min1 = 0;
                max1 = 50;
            } else {
                min1 = 0;
                max1 = 20;
            }
            return;
        }
        case 3: // km + m bzw. mi + yd
        { // 0 - 1000km (1000km ~ 621.37mi)
            if (min1 < min2) {
                min1 = 0;
                max1 = 1000;
                min2 = 0;
                max2 = 999;
            } else {
                min1 = 0;
                max1 = 500;
                min2 = 0;
                max2 = 1759;
            }
            Log.i("TestActivity", " 3) IS REACHED");
            return; // <= 3) IS REACHED
        }
        default:
            throw new RuntimeException("getMinMaxValuesForNumberPicker für "
                    + type + " nicht implementiert!");
        }
    }

日志清楚地说明了一切:

12-05 01:35:24.808: I/TestActivity(3784): Your method *******************
12-05 01:35:24.808: I/TestActivity(3784):  1) IS REACHED
12-05 01:35:24.808: I/TestActivity(3784):  2) IS REACHED

而且,还有两种测试方法可以使它更清晰,你可以尝试调试它们,它总是到达方法中的最后一个返回行

public static void testDebugingLine(int number) {
        Log.i("TestActivity", "Another example *******************");
        if (number < 0) {
            Log.e("TestActivity", "1 is reached");
            return;
        }

        Log.e("TestActivity", "2 is reached");
        return;
    }

    public static boolean testDebugingLine1(int number) {
        Log.i("TestActivity", "And another example *******************");
        if (number < 0) {
            Log.e("TestActivity", "1 is reached and return true");
            return true;
        }

        Log.e("TestActivity", "2 is reached and return false");
        return false;
    }


 testDebugingLine(-1);
 boolean result = testDebugingLine1(-1);
 Log.e("TestActivity", "testDebugingLine1() return " + result);

日志:

12-05 01:35:24.808: I/TestActivity(3784): Another example *******************
12-05 01:35:27.861: E/TestActivity(3784): 1 is reached
12-05 01:35:32.326: I/TestActivity(3784): And another example *******************
12-05 01:35:34.368: E/TestActivity(3784): 1 is reached and return true
12-05 01:35:42.497: E/TestActivity(3784): testDebugingLine1() return true

答案 2 :(得分:0)

问题出在代码snipplet之外。此外,我真的不知道你要用这​​个代码实现什么。除了浪费时间和调用getCurrentLengthUnit()之外,如果您处于其中一个案例中并且抛出异常,它绝对没有任何作用。所有赋值都写入LOCAL变量,对任何其他变量都没有影响。

您的代码与以下实现具有相同的效果:

public static void getMinMaxValuesForNumberPicker(LengthType type, Integer min1, Integer max1, Integer min2, Integer max2)
{

    switch (type)
    {
        case BODYSIZE:
        case CIRCUMFERENCE:
        case WRINKLE:
        case DISTANCE:
            getCurrentLengthUnit();
            break;
        default:
            throw new RuntimeException("getMinMaxValuesForNumberPicker für " + type.name() + " nicht implementiert!");
    }
}

因此,您可以从getMinMaxValuesForNumberPicker拨打getCurrentLengthUnit,或者您希望您的分配对您的函数不属于本地的变量产生影响。 写入参数不会使调用对调用函数可见:

Integer a=-1;
Integer b=-1;
Integer c=-1;
Integer d=-1;
getMinMaxValuesForNumberPicker(LengthType.BODYSIZE , a, b, c, d);
// a, b, c, d are still -1 here although you never assigned -1 and "assigned" null to the parameters in getMinMaxValuesForNumberPicker
// The parameters are just copies of the adresses of a, b, c and d and not aliases of a, b, c, and d

(我猜你没有使用调试器逐步完成代码。这可能会造成一些混乱。)

注意:自代码发布以来,问题已被编辑

相关问题