有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java switch语句不工作(“跳来跳去”很奇怪)

我使用一些非常简单的开关。。。但它不起作用。我只是看不出我的开关有任何错误

我在代码中添加了3条注释,以显示如果使用type == BODYSIZE输入,哪些点已达到或未达到

我不明白这怎么会发生,达到“1”和“2”是不可能的。。。但调试恰恰显示了这一点。。。它只是在“1”之后跳到“3”

我试着从我的手机上删除应用程序,删除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) 个答案

  1. # 1 楼答案

    问题不在您的代码snipplet范围内。此外,我真的不知道你想用这段代码实现什么。它除了浪费时间和调用getCurrentLengthUnit()(如果您遇到这种情况,则抛出异常)之外,什么也做不了。所有赋值都写入局部变量,不会对任何其他变量产生影响

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

    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!");
        }
    }
    

    因此,要么从getCurrentLengthUnit调用getMinMaxValuesForNumberPicker,要么期望赋值对函数的非局部变量产生影响。 写入参数不会使调用函数看到更改:

    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
    

    (我猜您没有使用调试器一步一步地检查代码。这可能会造成一些混乱。)

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

  2. # 2 楼答案

    事实上,你的代码没有问题,它工作得很好。我猜您使用的是eclipse,然后当您使用Android进行调试时,它总是转到方法中的最后一个返回行,它只是到达那里,但没有执行该行(这应该是eclipse的一个bug)

    我在代码中添加了一些日志,如下所示:

    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
    
  3. # 3 楼答案

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

    我的假设是,回报不会结束转换,但我对此不确定