2

我对这段代码有问题。它总是继续“你在博物馆外面或再次检查你的位置”祝酒:/你认为问题是什么?这是代码:

    x3 = x3 * -1; //for example is 187
    y3 = y3 * -1;//for example is 698

    ImageView img = (ImageView) findViewById(R.id.imageView1);

    if(((x3 <= 150) && (x3 >= 170)) && ((y3 <= 680) && (y3 >= 725))){
        img.setImageResource(R.drawable.map_1_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 1", Toast.LENGTH_LONG).show();
        currentSection = "sect_1";
    }else if(((x3 <= 170) && (x3 >= 195)) && ((y3 <= 690) && (y3 >= 715))){
        img.setImageResource(R.drawable.map_2_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 2", Toast.LENGTH_LONG).show(); //this suppost to be the output
        currentSection = "sect_2";
    }else if(((x3 <= 200) && (x3 >= 230)) && ((y3 <= 680) && (y3 >= 720))){
        img.setImageResource(R.drawable.map_3_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 3", Toast.LENGTH_LONG).show();
        currentSection = "sect_3";
    }else if(((x3 <= 190) && (x3 >= 220)) && ((y3 <= 675) && (y3 >= 710))){
        img.setImageResource(R.drawable.map_4_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 4", Toast.LENGTH_LONG).show();
        currentSection = "sect_4";
    }else if(((x3 <= 175) && (x3 >= 219)) && ((y3 <= 710) && (y3 >= 745))){
        img.setImageResource(R.drawable.map_5_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 5", Toast.LENGTH_LONG).show();
        currentSection = "sect_5";
    }else if(((x3 <= 155) && (x3 >= 165)) && ((y3 <= 715) && (y3 >= 735))){
        img.setImageResource(R.drawable.map_6_full_lite);
        Toast.makeText(getBaseContext(), "You're currently on Section 6", Toast.LENGTH_LONG).show();
        currentSection = "sect_6";
    }else{
        Toast.makeText(getBaseContext(), "You're outside of the museum or Check your postion again", Toast.LENGTH_LONG).show();
    }
4

3 回答 3

4

这是不可能的

(x3 <= 150) && (x3 >= 170)

原样

(x3 <= 170) && (x3 >= 195)

x3 不能小于或等于 150 并且大于或等于 170。我怀疑您打算这样

(150 <= x3) && (x3 <= 170)

(170 <= x3) && (x3 <= 195)
于 2013-01-28T08:43:24.527 回答
1

那是因为你的陈述如

(x3 <= 150) && (x3 >= 170)

永远不可能是真的。

你是这个意思吗?

(x3 <= 150) || (x3 >= 170)
于 2013-01-28T08:43:15.883 回答
1

你的条件总是false。只需考虑:

((x3 <= 155) && (x3 >= 165)) && ((y3 <= 715) && (y3 >= 735))

&&运算符意味着该运算符AND两侧的两个条件都必须为真,整个表达式才能为真。现在不难发现条件的任何值都是假的。因此,条件永远无法满足。x3(x3 <= 155) && (x3 >= 165)

您的每个分支都if具有该属性,因此最终else选择了该分支。

我认为您混合了<=(小于或等于)和>=(大于或等于)运算符。例如x3,所有值都应该在某个范围内(即房间边界坐标),就像在提到的情况下x3应该在范围内(x3 >= 155) && (x3 <= 165)(注意反向运算符)。

于 2013-01-28T08:46:49.963 回答