java - Can't understand why my || is not working -
i'm trying make simple game (even without interface). supposed type action (attack or block). why (enemy_hp > 0 || hp > 0
) statement doesn't work? loop goes forever.
public class game { public static void main(string[] args){ int hp = 10, enemy_hp = 10; string attack = "attack"; string block = "block"; scanner userinput = new scanner (system.in); while (enemy_hp > 0 || hp > 0) { system.out.println("it turn, attack or try block"); int your_block_chance1 = (int) (math.random() * 4); //chance block attack int enemy_block_chance1 = (int) (math.random() * 4); string action = userinput.next(); if (action.equals(attack)){ system.out.print("you attacked enemy , "); if (enemy_block_chance1 == 0) { system.out.println("he blocked it"); } else if (enemy_block_chance1 != 0){ enemy_hp = enemy_hp - 2; system.out.println("managed hit, hp " +enemy_hp); } } else if (action.equals(block)){ system.out.println("you dicided block"); your_block_chance1 = 0; } system.out.print("it enemy turn, decided "); int enemy_action = (int) (math.random() * 2); if (enemy_action == 1){ system.out.print("attack you,"); if (your_block_chance1 == 0){ system.out.println(" blocked it"); } else if (your_block_chance1 != 0){ hp = hp - 2; system.out.println(" , didn't block it, hp " +hp); } } else if (enemy_action != 1){ system.out.print("do heavy attack"); int heavy_attack_chance = (int) (math.random() * 2); if (heavy_attack_chance == 1){ system.out.println(" failed"); } else if (heavy_attack_chance != 1){ if (your_block_chance1 == 0){ system.out.println(" blocked it"); } else if (your_block_chance1 != 0){ hp = hp - 4; system.out.println(" , managed hit hard, hp " +hp); } } } } if (hp <= 0){ system.out.println("you failed"); } else if (enemy_hp <= 0){ system.out.println("you won!"); } } }
you want while stop when 1 of player die? should rather put &&. because || never false except if 2 players hp below 0.
Comments
Post a Comment