java - How can the code be stopped if a method returns true? -
public static void fbdeathcheck() { if (php <= 0) { } } private static int fbenemyhp = 10, fbenemystr = 2, fbenemydef = 2, fbenemyspd = 2, fbenemyatk = 1; public static void firstbattle(string[] args) { string nextmovefirstbattle = ""; int dodgechancefb = 0; while(fbenemyhp > 0 && php > 0) { system.out.print("\n\n\n\n\n\n\n\n\n\n\n\n\n"); system.out.println("-----battle mode-----"); system.out.println("enemy | hp " + fbenemyhp + " |"); if (nextmovefirstbattle.equalsignorecase("dodge") && dodgechancefb == 1) { system.out.println("dodged!"); } else { system.out.println(""); } system.out.println("commands:"); system.out.println("attack, dodge"); system.out.println(""); system.out.println("you | hp " + php + " |"); system.out.println("---------------------"); nextmovefirstbattle = scan.nextline(); while (!nextmovefirstbattle.equalsignorecase("attack") && !nextmovefirstbattle.equalsignorecase("dodge")) { nextmovefirstbattle = scan.nextline(); } if (nextmovefirstbattle.equalsignorecase("attack")) { if (fbenemyspd > pspeed){ php = php - fbenemyatk; //enemy attacks player fbenemyhp = fbenemyhp - patk; //player attacks enemy } else if (fbenemyspd < pspeed) { fbenemyhp = fbenemyhp - patk; //player attacks enemy php = php - fbenemyatk; //enemy attacks player } else { int chancefb = rand.nextint(2); if (chancefb == 0) { fbenemyhp = fbenemyhp - patk; //player attacks enemy php = php - fbenemyatk; //enemy attacks player } else if (chancefb == 1) { php = php - fbenemyatk; //enemy attacks player fbenemyhp = fbenemyhp - patk; //player attacks enemy } } } else if(nextmovefirstbattle.equalsignorecase("dodge")) { dodgechancefb = rand.nextint(10); if(dodgechancefb == 1) { php = php + 0; } else { php = php - fbenemyatk; //enemy hits player because dodge failed. } } } php = 15; }
basically, if put fbdeathcheck() after example,
php = php - fbenemyatk;
how make firstbattle() method stop if fbdeathcheck() true? purpose of fbdeathcheck() method stop battle when health points reach 0.
you call system.exit();
end jvm, end jvm, without giving warning or end screen. should have fbdeathcheck()
return boolean , have firstbattle()
check return value , end gracefully if needed
public static boolean fbdeathcheck() { if (php <= 0) { return true; } return false; } php = php - fbenemyatk; if (fbdeathcheck()) { // message battle ended return; }
Comments
Post a Comment