2

Im having a problem on this block of code. It always go on the "You're outside of the museum or Check your position again" toast :/ what do you think is the problem? Here is the code:

    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();
    }
2
  • 6
    x3 cannot be both less than 150 and greater than 170 simultaneously. Commented Jan 28, 2013 at 8:43
  • 1
    I would also like to mention that the program doesn't say "You're outside of the museum or Check your position again", ever. Commented Jan 28, 2013 at 8:53

3 Answers 3

4

This is impossible

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

as is

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

x3 cannot be less than or equal to 150 AND greater than or equal to 170. I suspect you intended something like this

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

and

(170 <= x3) && (x3 <= 195)
Sign up to request clarification or add additional context in comments.

6 Comments

You mean || in your last statement, didn't you?
Corrected it. I meant x3 is between [150, 170] I assume he has tiles the user can click on.
the logic is that if the x3 = 187(for example) is between of 150 x to 170 x and y3 = 693(for example) is between of 690 y and 715 y, then that coordinate is on that section.. something like that :O
How is 187 between 150 and 170?
I would write this as 150 <= x3 && x3 <= 170 && 680 <= y3 && y3 <= 725
|
1

You have conditions that are always false. Just consider:

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

the && operator is AND meaning both of the conditions on the sides of that operator must be true for the whole expression to be true. Now it is not hard to find out that for any value of x3 the condition (x3 <= 155) && (x3 >= 165) is false. Thus the condition can never be met.

Every branch of your if has that property, so in the end the else branch is chosen.

I think you have mixed the <= (less than or equal) and >= (more than or equal) operators. All the values, for example x3, should be in some range (i.e. room boundary coordinates), like in the mentioned case x3 should probably be in the range (x3 >= 155) && (x3 <= 165) (note reversed operators).

Comments

1

That's because your statements such as

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

can never be true.

Did you mean this?

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.