java - How to perferm different action depending on user input -
i'm trying make simple game (even without interface). supposed type action attack, heavy , light or block). "enemy" responds "light" action. problem?
import java.util.scanner; public class game { public static void main(string[] args) { int hp = 10, enemy_hp = 10; string attack = "attack"; string block = "block"; string heavy = "heavy"; string light = "light"; scanner userinput = new scanner(system.in); while (enemy_hp > 0 && hp > 0) { system.out.println("it turn, attack (heavy or light) 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)) { //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 - 3; system.out.println("managed hit, hp " + enemy_hp); } } else if (action.equals(light)) { //light attack system.out.print("you want light attack"); if (enemy_block_chance1 == 0) { system.out.println(" blocked it"); } else if (enemy_block_chance1 != 0) { enemy_hp = enemy_hp - 1; system.out.println(" , managed hit him, hp " + enemy_hp); } else if (action.equals(block)) { //block system.out.println("you dicided block , rest"); your_block_chance1 = (int)(math.random() * 1); hp++; } else if (action.equals(heavy)) { //heavy attack system.out.print("you went 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 (enemy_block_chance1 == 0) { system.out.println(" blocked it"); } else if (enemy_block_chance1 != 0) { enemy_hp = enemy_hp - 6; system.out.println(" , managed hit hard, hp " + enemy_hp); } } } system.out.print("it enemy turn, decided "); //enemy turn int enemy_action2 = (int)(math.random() * 4); if (enemy_action2 == 1) { system.out.print("attack you,"); //attack if (your_block_chance1 == 0) { system.out.println(" blocked it"); } else if (your_block_chance1 != 0) { hp = hp - 3; system.out.println(" , didn't block it, hp " + hp); } } else if (enemy_action2 == 0) { //heavy attack 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 - 6; system.out.println(" , managed hit hard, hp " + hp); } } } else if (enemy_action2 == 3) { //light attack system.out.print("do light attack"); if (your_block_chance1 == 0) { system.out.println(" blocked it"); } else if (your_block_chance1 != 0) { hp = hp - 1; system.out.println(" , managed hit you, hp " + hp); } } else if (enemy_action2 == 2) { //block system.out.println("block , rest"); enemy_hp++; } } if (hp <= 0) { system.out.println("you failed"); } else if (enemy_hp <= 0) { system.out.println("you won!"); } } } }
misplaced brackets: have removed end bracket }
end , added 1 before } else if (action.equals(heavy)) {
check this:
import java.util.scanner; public class game1 { public static void main(string[] args) { int hp = 10, enemy_hp = 10; string attack = "attack"; string block = "block"; string heavy = "heavy"; string light = "light"; scanner userinput = new scanner(system.in); while (enemy_hp > 0 && hp > 0) { system.out.println("it turn, attack (heavy or light) 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)) { //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 - 3; system.out.println("managed hit, hp " + enemy_hp); } } else if (action.equals(light)) { //light attack system.out.print("you want light attack"); if (enemy_block_chance1 == 0) { system.out.println(" blocked it"); } else if (enemy_block_chance1 != 0) { enemy_hp = enemy_hp - 1; system.out.println(" , managed hit him, hp " + enemy_hp); } } else if (action.equals(block)) { //block system.out.println("you dicided block , rest"); your_block_chance1 = (int)(math.random() * 1); hp++; } else if (action.equals(heavy)) { //heavy attack system.out.print("you went 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 (enemy_block_chance1 == 0) { system.out.println(" blocked it"); } else if (enemy_block_chance1 != 0) { enemy_hp = enemy_hp - 6; system.out.println(" , managed hit hard, hp " + enemy_hp); } } } system.out.print("it enemy turn, decided "); //enemy turn int enemy_action2 = (int)(math.random() * 4); if (enemy_action2 == 1) { system.out.print("attack you,"); //attack if (your_block_chance1 == 0) { system.out.println(" blocked it"); } else if (your_block_chance1 != 0) { hp = hp - 3; system.out.println(" , didn't block it, hp " + hp); } } else if (enemy_action2 == 0) { //heavy attack 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 - 6; system.out.println(" , managed hit hard, hp " + hp); } } } else if (enemy_action2 == 3) { //light attack system.out.print("do light attack"); if (your_block_chance1 == 0) { system.out.println(" blocked it"); } else if (your_block_chance1 != 0) { hp = hp - 1; system.out.println(" , managed hit you, hp " + hp); } } else if (enemy_action2 == 2) { //block system.out.println("block , rest"); enemy_hp++; } } if (hp <= 0) { system.out.println("you failed"); } else if (enemy_hp <= 0) { system.out.println("you won!"); } }
}
Comments
Post a Comment