简体   繁体   中英

If statement executing when conditions are false

if (((section[mapposition][mapdirection].getChanceNumber()==0)&&(section[mapposition][mapdirection].isWeaponFound()==false))&&
                (naturepart1.equals("rock"))||(naturepart2.equals("rock"))){
                // System.out.println(section[mapposition][mapdirection].getChanceNumber());
                // System.out.println(section[mapposition][mapdirection].isWeaponFound());
                player.addWeapon(section[mapposition][mapdirection].getWeapon());
                break;
}

Part of a program I'm making has an if statement like this. For some reason this block of code keeps executing even when "getChanceNumber()" returns the integer 2, and when "isWeaponFound()" returns true.

I have no clue why this if statement still executes even when the conditions are false.

Thanks for any help.

If naturepart2.equals("rock") is true, the statement will execute because of the || (or) between those conditions. Perhaps enclose those in parentheses to group them as a binary statement attached to an && along with your checks for getChanceNumber and isWeaponFound.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM